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

Fix S1125 FP: Type check for objects and dynamic types #8575

Merged

Conversation

zsolt-kolbay-sonarsource
Copy link
Contributor

Fixes #7792

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource force-pushed the Zsolt/Fix-S1125-FP-object-and-dynamic-types branch from 76862e0 to 162adf8 Compare January 22, 2024 11:51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the ImmutableArray directly without the copy. You could go even further with the caching, but I do not think it is worth the effort in this case.

@@ -25,6 +25,8 @@ public abstract class BooleanLiteralUnnecessaryBase<TSyntaxKind> : SonarDiagnost
{
internal const string DiagnosticId = "S1125";

private INamedTypeSymbol[] systemBooleanInterfaces;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private INamedTypeSymbol[] systemBooleanInterfaces;
private ImmutableArray<INamedTypeSymbol> systemBooleanInterfaces;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may also consider:

  • Loading the interfaces in CompilationStart and pass it from there or
  • Use a static ConditionalWeakTable<Compilation, ImmutableArray<INamedTypeSymbol>> to store the interfaces between the instances of the analyzer.

private bool TypeShouldBeIgnored(SyntaxNode node, SemanticModel model)
{
var type = model.GetTypeInfo(node).Type;
systemBooleanInterfaces ??= model.Compilation.GetTypeByMetadataName(KnownType.System_Boolean).Interfaces.ToArray();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
systemBooleanInterfaces ??= model.Compilation.GetTypeByMetadataName(KnownType.System_Boolean).Interfaces.ToArray();
systemBooleanInterfaces ??= model.Compilation.GetTypeByMetadataName(KnownType.System_Boolean).Interfaces;

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource force-pushed the Zsolt/Fix-S1125-FP-object-and-dynamic-types branch from 9369512 to 6c3bc93 Compare January 24, 2024 09:41
Copy link

sonarcloud bot commented Jan 24, 2024

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

Copy link

sonarcloud bot commented Jan 24, 2024

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

return type.IsNullableBoolean()
|| type is ITypeParameterSymbol
|| type.Is(KnownType.System_Object)
|| systemBooleanInterfaces.Any(x => x.Equals(type));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: Regarding symbol "Equals" there is some fun coming up, when we upgrade Roslyn:
dotnet/roslyn-analyzers#3427
Symbol equality comparison should be explicit about nullable annotation handling. Our version of Roslyn does not contain SymbolEqualityComparer.

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource merged commit 83269f7 into master Jan 24, 2024
27 checks passed
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource deleted the Zsolt/Fix-S1125-FP-object-and-dynamic-types branch January 24, 2024 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix S1125 FP: Type check with System.Object
2 participants