Skip to content

Commit 4ce46df

Browse files
martin-strecker-sonarsourceclaude
authored andcommitted
NET-3453 Formally verify compiler warnings in rule UTs
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> GitOrigin-RevId: 7488e0a265c1a181a232f7c1d5f7b935fdd10e27
1 parent 70f4199 commit 4ce46df

18 files changed

Lines changed: 90 additions & 70 deletions

analyzers/tests/SonarAnalyzer.Test/Rules/InvalidCastToInterfaceTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ namespace SonarAnalyzer.Test.Rules;
2323
public class InvalidCastToInterfaceTest
2424
{
2525
private readonly VerifierBuilder builderCS = new VerifierBuilder<CS.InvalidCastToInterface>(); // Syntax-based part of the rule, there also exists Sonar SE part
26-
private readonly VerifierBuilder builderVB = new VerifierBuilder<VB.InvalidCastToInterface>(); // Syntax-based part only
26+
27+
private readonly VerifierBuilder builderVB = new VerifierBuilder<VB.InvalidCastToInterface>() // Syntax-based part only
28+
.WithWarningsAsErrors("BC42322");
2729

2830
[TestMethod]
2931
[DataRow(ProjectType.Product)]

analyzers/tests/SonarAnalyzer.Test/Rules/LiteralSuffixUpperCaseTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ namespace SonarAnalyzer.Test.Rules;
2121
[TestClass]
2222
public class LiteralSuffixUpperCaseTest
2323
{
24-
private readonly VerifierBuilder builder = new VerifierBuilder<LiteralSuffixUpperCase>();
24+
private readonly VerifierBuilder builder = new VerifierBuilder<LiteralSuffixUpperCase>()
25+
.WithWarningsAsErrors("CS0078");
2526

2627
[TestMethod]
2728
public void LiteralSuffixUpperCase() =>

analyzers/tests/SonarAnalyzer.Test/Rules/ReferenceEqualityCheckWhenEqualsExistsTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class ReferenceEqualityCheckWhenEqualsExistsTest
2525
public void ReferenceEqualityCheckWhenEqualsExists() =>
2626
new VerifierBuilder<ReferenceEqualityCheckWhenEqualsExists>()
2727
.AddPaths("ReferenceEqualityCheckWhenEqualsExists.cs", "ReferenceEqualityCheckWhenEqualsExists2.cs")
28-
.WithAutogenerateConcurrentFiles(false).Verify();
28+
.WithAutogenerateConcurrentFiles(false)
29+
.WithWarningsAsErrors("CS0253")
30+
.Verify();
2931
}
3032
}

analyzers/tests/SonarAnalyzer.Test/Rules/UnchangedLocalVariablesShouldBeConstTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ public void UnchangedLocalVariablesShouldBeConst_TopLevelStatements() =>
5858
[TestMethod]
5959
public void UnchangedLocalVariablesShouldBeConst_Latest() =>
6060
verifier.AddPaths("UnchangedLocalVariablesShouldBeConst.Latest.cs")
61-
.WithOptions(LanguageOptions.CSharpLatest)
62-
.Verify();
61+
.WithOptions(LanguageOptions.CSharpLatest)
62+
.WithWarningsAsErrors("CS9193")
63+
.Verify();
6364

6465
[TestMethod]
6566
public void UnchangedLocalVariablesShouldBeConst_CshtmlIdeGenerated() =>

analyzers/tests/SonarAnalyzer.Test/Rules/UnnecessaryMathematicalComparisonTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ namespace SonarAnalyzer.Test.Rules;
2121
[TestClass]
2222
public class UnnecessaryMathematicalComparisonTest
2323
{
24-
private readonly VerifierBuilder builderCS = new VerifierBuilder<CS.UnnecessaryMathematicalComparison>();
24+
private readonly VerifierBuilder builderCS = new VerifierBuilder<CS.UnnecessaryMathematicalComparison>()
25+
.WithWarningsAsErrors("CS0652");
2526

2627
[TestMethod]
2728
public void UnnecessaryMathematicalComparison_CS() =>

analyzers/tests/SonarAnalyzer.Test/Rules/UnnecessaryUsingsTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public class UnnecessaryUsingsTest
2424
{
2525
private readonly VerifierBuilder builder = new VerifierBuilder<UnnecessaryUsings>()
2626
.AddReferences(MetadataReferenceFacade.MicrosoftWin32Primitives)
27-
.AddReferences(MetadataReferenceFacade.SystemSecurityCryptography);
27+
.AddReferences(MetadataReferenceFacade.SystemSecurityCryptography)
28+
.WithWarningsAsErrors("CS0105");
2829

2930
public TestContext TestContext { get; set; }
3031

analyzers/tests/SonarAnalyzer.Test/TestCases/InvalidCastToInterface.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Public Class Other
174174
o = DirectCast(IBar, IFoo)
175175
o = DirectCast(Bar, Foo) ' Compliant causes compiler error ' Error [BC30311] - invalid cast
176176
o = DirectCast(IBar, Foo)
177-
o = DirectCast(FinalBar, IFoo) ' Compliant causes compiler error ' Warning [BC42322] - invalid cast
177+
o = DirectCast(FinalBar, IFoo) ' Error [BC42322] - Compliant, causes compiler warning
178178
o = DirectCast(Generic, Bar) ' Compliant causes compiler error ' Error [BC30311] - invalid cast
179179

180180
o = TryCast(Bar, IFoo)

analyzers/tests/SonarAnalyzer.Test/TestCases/LiteralSuffixUpperCase.Fixed.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class LiteralSuffixUpperCase
1010
{
1111
public void Test(long ui)
1212
{
13+
// Error @+1 [CS0078] - compiler warning "The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity"
1314
const long b = 0L; // Fixed
1415
const ulong c = 0Ul;
1516
const ulong d = 0uL;
@@ -22,6 +23,7 @@ public void Test(long ui)
2223
const int k = 0; // Compliant
2324
const uint l = 0u;
2425

26+
// Error @+1 [CS0078] - compiler warning "The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity"
2527
Test(45L); // Fixed
2628
}
2729
public void TestOk()

analyzers/tests/SonarAnalyzer.Test/TestCases/LiteralSuffixUpperCase.Latest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
public void M()
66
{
7+
// Error @+1 [CS0078] - compiler warning "The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity"
78
(Property, var b) = (0l, // Noncompliant
9+
// Error @+1 [CS0078] - compiler warning "The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity"
810
0l); // Noncompliant
911
}
1012

analyzers/tests/SonarAnalyzer.Test/TestCases/LiteralSuffixUpperCase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class LiteralSuffixUpperCase
1010
{
1111
public void Test(long ui)
1212
{
13+
// Error @+1 [CS0078] - compiler warning "The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity"
1314
const long b = 0l; // Noncompliant {{Upper case this literal suffix.}}
1415
// ^
1516
const ulong c = 0Ul;
@@ -23,6 +24,7 @@ public void Test(long ui)
2324
const int k = 0; // Compliant
2425
const uint l = 0u;
2526

27+
// Error @+1 [CS0078] - compiler warning "The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity"
2628
Test(45l); // Noncompliant
2729
}
2830
public void TestOk()

0 commit comments

Comments
 (0)