diff --git a/src/Analyzers/MSTest.Analyzers/AssertionArgsShouldBePassedInCorrectOrderAnalyzer.cs b/src/Analyzers/MSTest.Analyzers/AssertionArgsShouldBePassedInCorrectOrderAnalyzer.cs index a675d0fc33..9d7e1f6aee 100644 --- a/src/Analyzers/MSTest.Analyzers/AssertionArgsShouldBePassedInCorrectOrderAnalyzer.cs +++ b/src/Analyzers/MSTest.Analyzers/AssertionArgsShouldBePassedInCorrectOrderAnalyzer.cs @@ -57,6 +57,9 @@ public override void Initialize(AnalysisContext context) }); } + private static bool IsConstant(IArgumentOperation argumentOperation) + => argumentOperation.Value.ConstantValue.HasValue; + private static void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol assertSymbol) { var invocationOperation = (IInvocationOperation)context.Operation; @@ -69,9 +72,10 @@ private static void AnalyzeOperation(OperationAnalysisContext context, INamedTyp return; } - // If the actual value is a constant or a literal, then the arguments are in the wrong order. - if (actualArgument.Value.Kind == OperationKind.Literal - || actualArgument.Value.ConstantValue.HasValue) + // If the actual value is a constant or a literal and expected is not, then the arguments are in the wrong order. + // Note that we don't report if both are literals or constants, as there is no real fix for this. + // If both are literals or constants, the assert will always pass or always fail. + if (IsConstant(actualArgument) && !IsConstant(expectedArgument)) { context.ReportDiagnostic(invocationOperation.CreateDiagnostic(Rule)); return; diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/AssertionArgsShouldBePassedInCorrectOrderAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/AssertionArgsShouldBePassedInCorrectOrderAnalyzerTests.cs index 2084f21415..7be33c066d 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/AssertionArgsShouldBePassedInCorrectOrderAnalyzerTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/AssertionArgsShouldBePassedInCorrectOrderAnalyzerTests.cs @@ -162,6 +162,28 @@ await VerifyCS.VerifyCodeFixAsync( fixedCode); } + [TestMethod] + public async Task WhenBothAreLiterals_NoDiagnostic() + { + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + using System.Collections.Generic; + + [TestClass] + public class MyTestClass + { + [TestMethod] + public void NonCompliant() + { + Assert.AreEqual(0, 0); + Assert.AreEqual(0, 1); + } + } + """; + + await VerifyCS.VerifyCodeFixAsync(code, code); + } + [TestMethod] public async Task LiteralUsingNamedArgument() {