Skip to content

Commit d290121

Browse files
committed
Updated after CR
1 parent 73aa130 commit d290121

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SX1101UnitTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,28 @@ public int this[int index]
199199
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
200200
}
201201

202+
/// <summary>
203+
/// Verifies that a necessary this prefix will not produce any diagnostics.
204+
/// </summary>
205+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
206+
[Fact]
207+
public async Task VerifyThatNecessaryPrefixWillNotProduceDiagnosticsAsync()
208+
{
209+
var testCode = @"
210+
public class TestClass
211+
{
212+
private int test;
213+
214+
public void TestMethod(int test)
215+
{
216+
this.test = test;
217+
}
218+
}
219+
";
220+
221+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
222+
}
223+
202224
/// <inheritdoc/>
203225
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
204226
{

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SX1101DoNotPrefixLocalMembersWithThis.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33

44
namespace StyleCop.Analyzers.ReadabilityRules
55
{
6+
using System;
7+
using System.Collections.Generic;
68
using System.Collections.Immutable;
9+
using System.Linq;
710
using Microsoft.CodeAnalysis;
811
using Microsoft.CodeAnalysis.CSharp;
12+
using Microsoft.CodeAnalysis.CSharp.Syntax;
913
using Microsoft.CodeAnalysis.Diagnostics;
1014

1115
[DiagnosticAnalyzer(LanguageNames.CSharp)]
@@ -42,8 +46,31 @@ private static void HandleThisExpression(SyntaxNodeAnalysisContext context)
4246
{
4347
if (context.Node.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression))
4448
{
45-
context.ReportDiagnostic(Diagnostic.Create(Descriptor, context.Node.GetLocation()));
49+
var memberAccessExpression = (MemberAccessExpressionSyntax)context.Node.Parent;
50+
var memberName = memberAccessExpression.Name.ToString();
51+
52+
var parameters = GetMethodParameters(context.Node);
53+
if (!parameters.Contains(memberName))
54+
{
55+
context.ReportDiagnostic(Diagnostic.Create(Descriptor, context.Node.GetLocation()));
56+
}
57+
}
58+
}
59+
60+
private static IList<string> GetMethodParameters(SyntaxNode node)
61+
{
62+
while ((node != null) && !node.IsKind(SyntaxKind.MethodDeclaration))
63+
{
64+
node = node.Parent;
4665
}
66+
67+
if (node == null)
68+
{
69+
return new List<string>();
70+
}
71+
72+
var methodDeclaration = (MethodDeclarationSyntax)node;
73+
return methodDeclaration.ParameterList.Parameters.Select(p => p.Identifier.ValueText).ToList();
4774
}
4875
}
4976
}

StyleCopAnalyzers.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "documentation", "documentat
217217
documentation\SA1650.md = documentation\SA1650.md
218218
documentation\SA1651.md = documentation\SA1651.md
219219
documentation\SA1652.md = documentation\SA1652.md
220-
SX1101.md = SX1101.md
220+
documentation\SX1101.md = documentation\SX1101.md
221221
documentation\SX1309.md = documentation\SX1309.md
222222
documentation\SX1309S.md = documentation\SX1309S.md
223223
EndProjectSection

SX1101.md renamed to documentation/SX1101.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ A call to an instance member of the local class or a base class is prefixed with
2121

2222
## Rule description
2323

24-
A violation of this rule occurs whenever the code contains a call to an instance member of the local class or a base class which is prefixed with `this.`.
24+
A violation of this rule occurs whenever the code contains a call to an instance member of the local class or a base class which is prefixed with `this.`. An exception is made when the name of a method parameter conflicts with identifier after the `this.` prefix, in that case no violation is reported.
2525
This rule is disabled by default. When enabling this rule, the [SA1101](SA1101.md) rule must be disabled.
2626

2727
## How to fix violations

0 commit comments

Comments
 (0)