diff --git a/analyzers/src/SonarAnalyzer.CSharp/Rules/DoNotUseLiteralBoolInAssertions.cs b/analyzers/src/SonarAnalyzer.CSharp/Rules/DoNotUseLiteralBoolInAssertions.cs index a0df7269203..8e38d56de85 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Rules/DoNotUseLiteralBoolInAssertions.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Rules/DoNotUseLiteralBoolInAssertions.cs @@ -29,29 +29,29 @@ public sealed class DoNotUseLiteralBoolInAssertions : SonarDiagnosticAnalyzer private const string MessageFormat = "Remove or correct this assertion."; private static readonly Dictionary> TrackedTypeAndMethods = - new () + new() { - [KnownType.Xunit_Assert] = new HashSet - { - // "True" is not here because there was no Assert.Fail in Xunit until 2020 and Assert.True(false) was a way to simulate it. - "Equal", "False", "NotEqual", "Same", "StrictEqual", "NotSame" - }, + [KnownType.Xunit_Assert] = + [ + // "True" and "False" are not here because there was no Assert.Fail in Xunit until 2020 and Assert.True(false) and Assert.False(true) were some ways to simulate it. + "Equal", "NotEqual", "Same", "StrictEqual", "NotSame" + ], - [KnownType.Microsoft_VisualStudio_TestTools_UnitTesting_Assert] = new HashSet - { + [KnownType.Microsoft_VisualStudio_TestTools_UnitTesting_Assert] = + [ "AreEqual", "AreNotEqual", "AreSame", "IsFalse", "IsTrue" - }, + ], - [KnownType.NUnit_Framework_Assert] = new HashSet - { + [KnownType.NUnit_Framework_Assert] = + [ "AreEqual", "AreNotEqual", "AreNotSame", "AreSame", "False", "IsFalse", "IsTrue", "That", "True" - }, + ], - [KnownType.System_Diagnostics_Debug] = new HashSet - { + [KnownType.System_Diagnostics_Debug] = + [ "Assert" - } + ] }; private static readonly ISet BoolLiterals = diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/DoNotUseLiteralBoolInAssertions.Xunit.cs b/analyzers/tests/SonarAnalyzer.Test/TestCases/DoNotUseLiteralBoolInAssertions.Xunit.cs index 95835b725ab..4ac16f16a31 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/DoNotUseLiteralBoolInAssertions.Xunit.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/DoNotUseLiteralBoolInAssertions.Xunit.cs @@ -9,7 +9,6 @@ public void Test() bool b = true; Xunit.Assert.Equal(true, b); // Noncompliant - Xunit.Assert.False(true); // Noncompliant Xunit.Assert.NotEqual(true, b); // Noncompliant Xunit.Assert.Same(true, b); // Noncompliant Xunit.Assert.StrictEqual(true, b); // Noncompliant @@ -19,9 +18,12 @@ public void Test() Xunit.Assert.Equal(true, false); // Noncompliant Xunit.Assert.Equal(b, b); - Xunit.Assert.True(true); - // There is no Assert.Fail in Xunit. Assert.True(false) is way to simulate it. - Xunit.Assert.True(false); + Xunit.Assert.True(true); // FN + Xunit.Assert.False(false); // FN + + // There is no Assert.Fail in Xunit. Assert.True(false) or Assert.False(true) are ways to simulate it. + Xunit.Assert.True(false); // Compliant + Xunit.Assert.False(true); // Compliant bool? x = false; Xunit.Assert.Equal(false, x); // Compliant, since the comparison triggers a conversion