Skip to content

Commit 3b18b2f

Browse files
committed
Add tests and additional cases to SA1314
1 parent 7e8d738 commit 3b18b2f

2 files changed

Lines changed: 92 additions & 39 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/RenameHelper.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ public static async Task<bool> IsValidNewMemberNameAsync(SemanticModel semanticM
5454

5555
if (symbol.Kind == SymbolKind.TypeParameter)
5656
{
57-
// If the symbol is a type parameter, the name can't be the same any type parameters
58-
// of the containing type.
57+
// If the symbol is a type parameter, the name can't be the same as any type parameters of the containing type.
5958
var parentSymbol = containingSymbol?.ContainingSymbol as INamedTypeSymbol;
60-
if (parentSymbol == null)
59+
if (parentSymbol != null
60+
&& parentSymbol.TypeParameters.Any(t => t.Name == name))
6161
{
62-
return true;
62+
return false;
6363
}
6464

65-
bool conflict = parentSymbol.TypeParameters.Any(t => t.Name == name);
66-
return !conflict;
65+
// Move up one level for the next validation step.
66+
containingSymbol = containingSymbol?.ContainingSymbol;
6767
}
6868

6969
var containingNamespaceOrTypeSymbol = containingSymbol as INamespaceOrTypeSymbol;
@@ -144,30 +144,30 @@ public static SyntaxNode GetParentDeclaration(SyntaxToken token)
144144
{
145145
switch (parent.Kind())
146146
{
147-
case SyntaxKind.VariableDeclarator:
148-
case SyntaxKind.Parameter:
149-
case SyntaxKind.TypeParameter:
150-
case SyntaxKind.CatchDeclaration:
151-
case SyntaxKind.ExternAliasDirective:
152-
case SyntaxKind.QueryContinuation:
153-
case SyntaxKind.FromClause:
154-
case SyntaxKind.LetClause:
155-
case SyntaxKind.JoinClause:
156-
case SyntaxKind.JoinIntoClause:
157-
case SyntaxKind.ForEachStatement:
158-
case SyntaxKind.UsingDirective:
159-
case SyntaxKind.LabeledStatement:
160-
case SyntaxKind.AnonymousObjectMemberDeclarator:
161-
return parent;
162-
163-
default:
164-
var declarationParent = parent as MemberDeclarationSyntax;
165-
if (declarationParent != null)
166-
{
167-
return declarationParent;
168-
}
169-
170-
break;
147+
case SyntaxKind.VariableDeclarator:
148+
case SyntaxKind.Parameter:
149+
case SyntaxKind.TypeParameter:
150+
case SyntaxKind.CatchDeclaration:
151+
case SyntaxKind.ExternAliasDirective:
152+
case SyntaxKind.QueryContinuation:
153+
case SyntaxKind.FromClause:
154+
case SyntaxKind.LetClause:
155+
case SyntaxKind.JoinClause:
156+
case SyntaxKind.JoinIntoClause:
157+
case SyntaxKind.ForEachStatement:
158+
case SyntaxKind.UsingDirective:
159+
case SyntaxKind.LabeledStatement:
160+
case SyntaxKind.AnonymousObjectMemberDeclarator:
161+
return parent;
162+
163+
default:
164+
var declarationParent = parent as MemberDeclarationSyntax;
165+
if (declarationParent != null)
166+
{
167+
return declarationParent;
168+
}
169+
170+
break;
171171
}
172172

173173
parent = parent.Parent;

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1314UnitTests.cs

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace StyleCop.Analyzers.Test.NamingRules
1515
public class SA1314UnitTests : CodeFixVerifier
1616
{
1717
[Fact]
18-
public async Task TestGenericParameterDoesNotStartWithTAsync()
18+
public async Task TestTypeParameterDoesNotStartWithTAsync()
1919
{
2020
var testCode = @"
2121
public interface IFoo<Key>
@@ -35,7 +35,7 @@ public interface IFoo<TKey>
3535
}
3636

3737
[Fact]
38-
public async Task TestGenericParameterDoesNotStartWithTPlusParameterUsedAsync()
38+
public async Task TestTypeParameterDoesNotStartWithTPlusParameterUsedAsync()
3939
{
4040
var testCode = @"
4141
public class Foo<Key>
@@ -65,7 +65,7 @@ void Test()
6565
}
6666

6767
[Fact]
68-
public async Task TestGenericParameterStartsWithLowerTAsync()
68+
public async Task TestTypeParameterStartsWithLowerTAsync()
6969
{
7070
var testCode = @"
7171
public interface IFoo<tKey>
@@ -85,7 +85,7 @@ public interface IFoo<TtKey>
8585
}
8686

8787
[Fact]
88-
public async Task TestInnerGenericParameterDoesNotStartWithTAsync()
88+
public async Task TestInnerTypeParameterDoesNotStartWithTAsync()
8989
{
9090
var testCode = @"
9191
public class Bar
@@ -111,7 +111,7 @@ public class Foo<TKey>
111111
}
112112

113113
[Fact]
114-
public async Task TestGenericParameterDoesStartWithTAsync()
114+
public async Task TestTypeParameterDoesStartWithTAsync()
115115
{
116116
var testCode = @"public interface IFoo<TKey>
117117
{
@@ -121,7 +121,7 @@ public async Task TestGenericParameterDoesStartWithTAsync()
121121
}
122122

123123
[Fact]
124-
public async Task TestInnerGenericParameterDoesStartWithTAsync()
124+
public async Task TestInnerTypeParameterDoesStartWithTAsync()
125125
{
126126
var testCode = @"
127127
public class Bar
@@ -135,7 +135,7 @@ public class Foo<TKey>
135135
}
136136

137137
[Fact]
138-
public async Task TestGenericParameterDoesNotStartWithTWithMemberMatchingTargetTypeAsync()
138+
public async Task TestTypeParameterDoesNotStartWithTWithMemberMatchingTargetTypeAsync()
139139
{
140140
string testCode = @"
141141
public class Foo<Key>
@@ -157,7 +157,7 @@ public class Foo<TKey>
157157
}
158158

159159
[Fact]
160-
public async Task TestNestedGenericParameterDoesNotStartWithTWithConflictAsync()
160+
public async Task TestNestedTypeParameterDoesNotStartWithTWithConflictAsync()
161161
{
162162
string testCode = @"
163163
public class Outer<TKey>
@@ -182,7 +182,7 @@ public class Foo<TKey1>
182182
}
183183

184184
[Fact]
185-
public async Task TestNestedGenericParameterDoesNotStartWithTWithMemberConflictAsync()
185+
public async Task TestNestedTypeParameterDoesNotStartWithTWithMemberConflictAsync()
186186
{
187187
string testCode = @"
188188
public class Outer<TKey>
@@ -208,6 +208,59 @@ public class Foo<TKey1>
208208
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
209209
}
210210

211+
[Fact]
212+
public async Task TestTypeParameterDoesNotStartWithTAndTypeConflictAsync()
213+
{
214+
string testCode = @"
215+
public class TFoo
216+
{
217+
}
218+
219+
public class Bar<Foo>
220+
{
221+
}";
222+
string fixedCode = @"
223+
public class TFoo
224+
{
225+
}
226+
227+
public class Bar<TFoo1>
228+
{
229+
}";
230+
231+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(6, 18);
232+
233+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
234+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
235+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
236+
}
237+
238+
[Fact]
239+
public async Task TestTypeParameterInMethodSignatureDoesNotStartWithTAsync()
240+
{
241+
var testCode = @"
242+
public class Foo
243+
{
244+
public void Bar<Baz>()
245+
{
246+
}
247+
}";
248+
249+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(4, 21);
250+
251+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
252+
253+
var fixedCode = @"
254+
public class Foo
255+
{
256+
public void Bar<TBaz>()
257+
{
258+
}
259+
}";
260+
261+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
262+
}
263+
211264
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
212265
{
213266
yield return new SA1314TypeParameterNamesMustBeginWithT();

0 commit comments

Comments
 (0)