Skip to content

Commit

Permalink
Do not make generic class static if it's inherited (RCS1102) (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Oct 31, 2022
1 parent 545aaa1 commit b11a3ed
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Use explicit type from lambda expression ([RCS1008](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1008.md)) ([#967](https://github.com/josefpihrt/roslynator/pull/967).
- Do not remove constructor if it is decorated with 'UsedImplicitlyAttribute' ([RCS1074](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1074.md)) ([#968](https://github.com/josefpihrt/roslynator/pull/968).
- Detect argument null check in the form of `ArgumentNullException.ThrowIfNull` ([RR0025](https://github.com/JosefPihrt/Roslynator/blob/main/docs/refactorings/RR0025.md), [RCS1227](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1227.md)) ([#974](https://github.com/josefpihrt/roslynator/pull/974).
- Do not make generic class static if it's inherited ([RCS1102](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1102.md)) ([#978](https://github.com/josefpihrt/roslynator/pull/978).

-----
<!-- Content below does not adhere to 'Keep a Changelog' format -->
Expand Down
15 changes: 12 additions & 3 deletions src/Analyzers/CSharp/Analysis/MakeClassStaticAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,27 @@ protected override void VisitType(TypeSyntax node)
{
if (node is IdentifierNameSyntax identifierName)
{
if (string.Equals(Symbol.Name, identifierName.Identifier.ValueText, StringComparison.Ordinal)
if (!identifierName.IsVar
&& string.Equals(Symbol.Name, identifierName.Identifier.ValueText, StringComparison.Ordinal)
&& SymbolEqualityComparer.Default.Equals(
SemanticModel.GetSymbol(identifierName, CancellationToken)?.OriginalDefinition,
Symbol))
{
CanBeMadeStatic = false;
}
}
else
else if (node is GenericNameSyntax genericName)
{
base.VisitType(node);
if (string.Equals(Symbol.Name, genericName.Identifier.ValueText, StringComparison.Ordinal)
&& SymbolEqualityComparer.Default.Equals(
SemanticModel.GetSymbol(genericName, CancellationToken)?.OriginalDefinition,
Symbol))
{
CanBeMadeStatic = false;
}
}

base.VisitType(node);
}

public static MakeClassStaticWalker GetInstance()
Expand Down
13 changes: 13 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1102MakeClassStaticTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ static C M()
return null;
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.MakeClassStatic)]
public async Task TestNoDiagnostic_NestedClass()
{
await VerifyNoDiagnosticAsync(@"
public class Class1<T>
{
public sealed class Class2 : Class1<T>
{
}
}
");
}
}
Expand Down

0 comments on commit b11a3ed

Please sign in to comment.