Skip to content

Commit 8431280

Browse files
alex-meseldzija-sonarsourceSonarTech
authored andcommitted
NET-3376 Fix S6930 AD0001: Issue on template / code files for blazor
GitOrigin-RevId: 824736d883bc02f47c4c9de4fc606df6df9381cf
1 parent 21af635 commit 8431280

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

analyzers/src/SonarAnalyzer.Shared/Syntax/Extensions/MemberAccessExpressionSyntaxExtensionsShared.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ namespace SonarAnalyzer.VisualBasic.Core.Syntax.Extensions;
2323
internal static class MemberAccessExpressionSyntaxExtensionsShared
2424
{
2525
public static bool IsPtrZero(this MemberAccessExpressionSyntax memberAccess, SemanticModel model) =>
26-
memberAccess.Name.Identifier.Text == "Zero"
27-
&& model.GetTypeInfo(memberAccess).Type is var type
26+
memberAccess.Name.Identifier.Text == nameof(IntPtr.Zero)
27+
&& memberAccess.EnsureCorrectSemanticModelOrDefault(model) is { } maModel
28+
&& maModel.GetTypeInfo(memberAccess).Type is var type
2829
&& type.IsAny(KnownType.System_IntPtr, KnownType.System_UIntPtr);
2930
}

analyzers/tests/SonarAnalyzer.Test/Rules/AspNet/BackslashShouldBeAvoidedInAspNetRoutesTest.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,39 @@ private static IEnumerable<MetadataReference> AspNetCore2xReferences(string aspN
141141
AspNetCoreMetadataReference.MicrosoftAspNetCoreRouting, // For IEndpointRouteBuilder
142142
];
143143

144+
[TestMethod]
145+
public void BackslashShouldBeAvoidedInAspNetRoutes_CrossFilePartialClass_DoesNotThrow() =>
146+
builderCS
147+
.AddReferences(AspNetCore3AndAboveReferences)
148+
.AddSnippet("""
149+
using System;
150+
151+
public partial class SomeComponent
152+
{
153+
private TimeSpan _elapsedTime = TimeSpan.Zero;
154+
}
155+
""")
156+
.AddSnippet("""
157+
using Microsoft.AspNetCore.Mvc;
158+
using System;
159+
160+
public class MyController : Controller
161+
{
162+
public IActionResult Index() => View();
163+
}
164+
165+
public partial class SomeComponent
166+
{
167+
public void Process()
168+
{
169+
Use(_elapsedTime);
170+
}
171+
172+
private void Use(object value) { }
173+
}
174+
""")
175+
.VerifyNoIssues();
176+
144177
[TestMethod]
145178
[DynamicData(nameof(AspNetCore2xVersionsUnderTest))]
146179
public void BackslashShouldBeAvoidedInAspNetRoutes_AspNetCore2x_CS(string aspNetCoreVersion) =>

0 commit comments

Comments
 (0)