Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not make generic class static if it's inherited (RCS1102) #978

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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