Skip to content

Commit

Permalink
Rule S4226: Add exception for types decorated with GeneratedCode attr…
Browse files Browse the repository at this point in the history
…ibute
  • Loading branch information
costin-zaharia-sonarsource committed Mar 14, 2022
1 parent 8385ae9 commit f5b1deb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions analyzers/rspec/cs/S4226_c#.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ <h2>Compliant Solution</h2>
}
}
</pre>
<h2>Exceptions</h2>
<ul>
<li> Extension methods added for clases decorated with <code>System.CodeDom.Compiler.GeneratedCodeAttribute</code> attribute. </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ protected override void Initialize(SonarAnalysisContext context) =>
&& c.SemanticModel.GetDeclaredSymbol(methodDeclaration) is { IsExtensionMethod: true, Parameters: { Length: > 0 } } methodSymbol
&& methodSymbol.Parameters[0].Type.Kind != SymbolKind.ErrorType
&& methodSymbol.Parameters[0].Type.IsClass()
&& methodSymbol.ContainingNamespace.Equals(methodSymbol.Parameters[0].Type.ContainingNamespace))
&& methodSymbol.ContainingNamespace.Equals(methodSymbol.Parameters[0].Type.ContainingNamespace)
&& !methodSymbol.Parameters[0].Type.HasAttribute(KnownType.System_CodeDom_Compiler_GeneratedCodeAttribute))
{
c.ReportIssue(Diagnostic.Create(Rule, methodDeclaration.Identifier.GetLocation()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ <h2>Compliant Solution</h2>
}
}
</pre>
<h2>Exceptions</h2>
<ul>
<li> Extension methods added for clases decorated with <code>System.CodeDom.Compiler.GeneratedCodeAttribute</code> attribute. </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ public static T FooGeneric<T>(this T foo) // Compliant

internal static class GenClassExtensions
{
public static void SetSyncLaterError(this GenClass foo) { } // Noncompliant - FP, see: https://github.com/SonarSource/sonar-dotnet/issues/5457
public static void SetSyncLaterError(this GenClass foo) { } // Compliant, see: https://github.com/SonarSource/sonar-dotnet/issues/5457
}

0 comments on commit f5b1deb

Please sign in to comment.