Skip to content

Commit

Permalink
Refine diagnostic for switch expression that is not exhaustive
Browse files Browse the repository at this point in the history
  • Loading branch information
gafter committed Sep 16, 2019
1 parent 42d4dd7 commit 86c7d0d
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5661,10 +5661,10 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>The syntax 'var' for a pattern is not permitted to refer to a type, but '{0}' is in scope here.</value>
</data>
<data name="WRN_SwitchExpressionNotExhaustive" xml:space="preserve">
<value>The switch expression does not handle all possible inputs (it is not exhaustive).</value>
<value>The switch expression does not handle all possible values of its input type (it is not exhaustive).</value>
</data>
<data name="WRN_SwitchExpressionNotExhaustive_Title" xml:space="preserve">
<value>The switch expression does not handle all possible inputs (it is not exhaustive).</value>
<value>The switch expression does not handle all possible values of its input type (it is not exhaustive).</value>
</data>
<data name="WRN_CaseConstantNamedUnderscore" xml:space="preserve">
<value>The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name.</value>
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Test/Emit/CodeGen/PatternTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3026,7 +3026,7 @@ static class C {
";
var compilation = CreateEmptyCompilation(source, options: TestOptions.ReleaseDll);
compilation.GetDiagnostics().Verify(
// (9,38): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (9,38): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// public static bool M(int i) => i switch { 1 => true };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 38)
);
Expand All @@ -3036,7 +3036,7 @@ static class C {
// (9,36): error CS0656: Missing compiler required member 'System.InvalidOperationException..ctor'
// public static bool M(int i) => i switch { 1 => true };
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "i switch { 1 => true }").WithArguments("System.InvalidOperationException", ".ctor").WithLocation(9, 36),
// (9,38): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (9,38): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// public static bool M(int i) => i switch { 1 => true };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 38)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void M(int? x, object y)
Arms(0)
";
var expectedDiagnostics = new[] {
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// y = /*<bind>*/x switch { }/*</bind>*/;
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25)
};
Expand Down Expand Up @@ -110,7 +110,7 @@ void M(int? x, object y)
ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 5) (Syntax: '5')
";
var expectedDiagnostics = new[] {
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// y = /*<bind>*/x switch { => 5 }/*</bind>*/;
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25),
// file.cs(7,34): error CS8504: Pattern missing
Expand Down Expand Up @@ -355,7 +355,7 @@ void M(int? x, object y)
ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 5) (Syntax: '5')
";
var expectedDiagnostics = new[] {
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// y = /*<bind>*/x switch { NotFound => 5 }/*</bind>*/;
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25),
// file.cs(7,34): error CS0103: The name 'NotFound' does not exist in the current context
Expand Down Expand Up @@ -512,7 +512,7 @@ void M(int? x, object y)
ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 5) (Syntax: '5')
";
var expectedDiagnostics = new[] {
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// y = /*<bind>*/x switch { 1 => 2, _ when false => 5 }/*</bind>*/;
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25)
};
Expand Down Expand Up @@ -589,7 +589,7 @@ void M(int? x, object y)
ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 5) (Syntax: '5')
";
var expectedDiagnostics = new[] {
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// y = /*<bind>*/x switch { _ when NotFound => 5 }/*</bind>*/;
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25),
// file.cs(7,41): error CS0103: The name 'NotFound' does not exist in the current context
Expand Down Expand Up @@ -636,7 +636,7 @@ void M(int? x, object y)
Local_2: System.Int32 z
";
var expectedDiagnostics = new[] {
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// y = /*<bind>*/x switch { int z when x is int z => 5 }/*</bind>*/;
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25),
// file.cs(7,54): error CS0128: A local variable or function named 'z' is already defined in this scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3324,7 +3324,7 @@ public static void Main()
}
";
var expectedDiagnostics = new[] {
// file.cs(6,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// file.cs(6,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// var r = 1 switch { };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(6, 19),
// file.cs(6,19): error CS8506: No best type was found for the switch expression.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public static void Main()
}
}";
CreatePatternCompilation(source).VerifyDiagnostics(
// (5,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (5,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// var r = 1 switch { };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(5, 19),
// (5,19): error CS8506: No best type was found for the switch expression.
Expand All @@ -518,7 +518,7 @@ public static void M() {}
public delegate void D();
}";
CreatePatternCompilation(source).VerifyDiagnostics(
// (5,19): warning CS8409: The switch expression does not handle all possible inputs (it is not exhaustive).
// (5,19): warning CS8409: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// var x = 1 switch { 0 => M, 1 => new D(M), 2 => M };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(5, 19)
);
Expand Down Expand Up @@ -609,7 +609,7 @@ public static void Main()
}";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (7,19): warning CS8409: The switch expression does not handle all possible inputs (it is not exhaustive).
// (7,19): warning CS8409: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// var c = a switch { var x2 when x2 is var x3 => x3 };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 19)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ ref int M(bool b, ref int x, ref int y)
}";
var compilation = CreateCompilationWithMscorlibAndSpan(source, options: TestOptions.DebugDll);
compilation.VerifyDiagnostics(
// (6,23): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (6,23): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// return ref (b switch { true => ref x, false => ref y });
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(6, 23),
// (6,40): error CS1525: Invalid expression term 'ref'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ static int M1(bool? b1, bool? b2)
";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (6,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (6,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// return (b1, b2) switch {
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(6, 25)
);
Expand Down Expand Up @@ -2048,7 +2048,7 @@ static void Main()
";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (9,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (9,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// _ = t switch { (3, 4) => 1 };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 19)
);
Expand Down Expand Up @@ -2091,7 +2091,7 @@ public SwitchExpressionException() {}
";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (9,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (9,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// _ = t switch { (3, 4) => 1 };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 19)
);
Expand Down Expand Up @@ -2134,7 +2134,7 @@ public class SwitchExpressionException : InvalidOperationException
";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (9,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (9,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// _ = t switch { (3, 4) => 1 };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 19)
);
Expand Down Expand Up @@ -2176,7 +2176,7 @@ public class SwitchExpressionException : InvalidOperationException
";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (8,24): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (8,24): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// _ = (1, 2) switch { (3, 4) => 1 };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(8, 24)
);
Expand Down Expand Up @@ -2223,7 +2223,7 @@ public SwitchExpressionException() {}
";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (9,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (9,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// _ = r switch { (3, 4) => 1 };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 19)
);
Expand Down Expand Up @@ -2815,7 +2815,7 @@ public static void Main()
";
var compilation = CreatePatternCompilation(source, options: TestOptions.ReleaseExe);
compilation.VerifyDiagnostics(
// (7,32): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (7,32): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// Console.Write((x, 300) switch { ((1, int x2), int y) => x2+y });
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 32)
);
Expand Down Expand Up @@ -2891,7 +2891,7 @@ public static void Main()
";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (6,15): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (6,15): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// _ = o switch { null => 1 };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(6, 15)
);
Expand Down Expand Up @@ -2930,7 +2930,7 @@ public int M(bool b)
";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (22,18): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (22,18): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// return b switch
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(22, 18)
);
Expand Down Expand Up @@ -3082,7 +3082,7 @@ public class SwitchExpressionException : InvalidOperationException
";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (17,23): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (17,23): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// return (x, y) switch { (1, 2) => 3 };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(17, 23)
);
Expand Down Expand Up @@ -3218,7 +3218,7 @@ public class SwitchExpressionException : InvalidOperationException
";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (17,44): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
// (17,44): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
// return (x, y, a, b, c, d, e, f, g) switch { (1, 2, _, _, _, _, _, _, _) => 3 };
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(17, 44)
);
Expand Down

0 comments on commit 86c7d0d

Please sign in to comment.