From 5fb5a91b24c523f2b64da5b34c02b41243dff853 Mon Sep 17 00:00:00 2001 From: Pavel Mikula <57188685+pavel-mikula-sonarsource@users.noreply.github.com> Date: Mon, 27 May 2024 14:41:08 +0200 Subject: [PATCH] Fix S2234 Bug: AD0001 is thrown due to referencing a location outside of the current compilation (#9323) --- .../Rules/ParametersCorrectOrderBase.cs | 4 ++-- .../Rules/ParametersCorrectOrderTest.cs | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.Common/Rules/ParametersCorrectOrderBase.cs b/analyzers/src/SonarAnalyzer.Common/Rules/ParametersCorrectOrderBase.cs index ea42b6882c1..d22faf717e7 100644 --- a/analyzers/src/SonarAnalyzer.Common/Rules/ParametersCorrectOrderBase.cs +++ b/analyzers/src/SonarAnalyzer.Common/Rules/ParametersCorrectOrderBase.cs @@ -56,9 +56,9 @@ protected sealed override void Initialize(SonarAnalysisContext context) => && Language.Syntax.ArgumentList(c.Node).FirstOrDefault(x => MatchingNames(parameterSymbol, ArgumentName(x))) is { }) { var secondaryLocations = methodParameterLookup.MethodSymbol.DeclaringSyntaxReferences - .Select(s => Language.Syntax.NodeIdentifier(s.GetSyntax())?.GetLocation()) + .Select(x => Language.Syntax.NodeIdentifier(x.GetSyntax())?.ToSecondaryLocation()) .WhereNotNull(); - c.ReportIssue(Diagnostic.Create(Rule, PrimaryLocation(c.Node), secondaryLocations, properties: null, methodParameterLookup.MethodSymbol.Name)); + c.ReportIssue(Rule, PrimaryLocation(c.Node), secondaryLocations, methodParameterLookup.MethodSymbol.Name); return; } } diff --git a/analyzers/tests/SonarAnalyzer.Test/Rules/ParametersCorrectOrderTest.cs b/analyzers/tests/SonarAnalyzer.Test/Rules/ParametersCorrectOrderTest.cs index fe5b66b02ba..62bff2f4314 100644 --- a/analyzers/tests/SonarAnalyzer.Test/Rules/ParametersCorrectOrderTest.cs +++ b/analyzers/tests/SonarAnalyzer.Test/Rules/ParametersCorrectOrderTest.cs @@ -73,4 +73,27 @@ Public Sub Bar() End Sub End Class """).VerifyNoIssuesIgnoreErrors(); + + [TestMethod] + public async Task ParametersCorrectOrder_SecondaryLocationsOutsideCurrentCompilation() + { + var library = TestHelper.CompileCS(""" + public static class Library + { + public static void Method(int a, int b) { } + } + """).Model.Compilation; + var usage = TestHelper.CompileCS(""" + public class Usage + { + public void Method() + { + int a = 4, b = 2; + Library.Method(b, a); + } + } + """, library.ToMetadataReference()).Model.Compilation; + var diagnostics = await usage.WithAnalyzers([new CS.ParametersCorrectOrder()]).GetAnalyzerDiagnosticsAsync(); + diagnostics.Should().ContainSingle().Which.Id.Should().Be("S2234", "we don't want AD0001 here"); + } }