diff --git a/CodeBlocker.Test/ScopesTests.cs b/CodeBlocker.Test/ScopesTests.cs
index 51e9788..86716eb 100644
--- a/CodeBlocker.Test/ScopesTests.cs
+++ b/CodeBlocker.Test/ScopesTests.cs
@@ -135,6 +135,39 @@ public void PragmaScopeJoinsSeveralWarnings()
codeBlocker.ToString());
}
+ [TestMethod]
+ public void PragmaScopeWithAnEmptyStringIsANoOp()
+ {
+ using CodeBlocker codeBlocker = Create();
+
+ using (new PragmaScope(codeBlocker, ""))
+ {
+ codeBlocker.WriteLine("public int Value;");
+ }
+
+ Assert.AreEqual("public int Value;\n", codeBlocker.ToString());
+ }
+
+ [TestMethod]
+ public void PragmaScopeWithAnEmptyWarningSequenceDoesNotAffectAnOuterScope()
+ {
+ using CodeBlocker codeBlocker = Create();
+
+ using (new PragmaScope(codeBlocker, "CS1591"))
+ {
+ using (new PragmaScope(codeBlocker, []))
+ {
+ codeBlocker.WriteLine("public int X;");
+ }
+
+ codeBlocker.WriteLine("public int Y;");
+ }
+
+ Assert.AreEqual(
+ "#pragma warning disable CS1591\npublic int X;\npublic int Y;\n#pragma warning restore CS1591\n",
+ codeBlocker.ToString());
+ }
+
[TestMethod]
public void ScopesOfMixedKindsNestCorrectly()
{
diff --git a/CodeBlocker/Scopes.cs b/CodeBlocker/Scopes.cs
index 170af82..e293b07 100644
--- a/CodeBlocker/Scopes.cs
+++ b/CodeBlocker/Scopes.cs
@@ -159,7 +159,8 @@ private static void End(CodeBlocker codeBlocker)
/// The parent .
///
/// The warning identifiers to suppress, written verbatim after the directive — either a single
-/// identifier such as CS1591 or a comma-separated list.
+/// identifier such as CS1591 or a comma-separated list. Empty or whitespace warnings are a
+/// no-op and emit no directives.
///
public class PragmaScope(CodeBlocker codeBlocker, string warnings)
: ScopedAction(onOpen: () => Begin(codeBlocker, warnings), onClose: () => End(codeBlocker, warnings))
@@ -177,12 +178,22 @@ public PragmaScope(CodeBlocker codeBlocker, IEnumerable warnings)
private static void Begin(CodeBlocker codeBlocker, string warnings)
{
Ensure.NotNull(codeBlocker);
+ if (string.IsNullOrWhiteSpace(warnings))
+ {
+ return;
+ }
+
codeBlocker.WriteLine($"#pragma warning disable {warnings}");
}
private static void End(CodeBlocker codeBlocker, string warnings)
{
Ensure.NotNull(codeBlocker);
+ if (string.IsNullOrWhiteSpace(warnings))
+ {
+ return;
+ }
+
codeBlocker.WriteLine($"#pragma warning restore {warnings}");
}
}