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

Rule S6602: Update the message for arrays #8956

Merged
merged 3 commits into from
Mar 20, 2024

Conversation

costin-zaharia-sonarsource
Copy link
Member

Fixes #8935

@@ -48,7 +50,8 @@ public abstract class FindInsteadOfFirstOrDefaultBase<TSyntaxKind, TInvocationEx
&& IsCorrectType(left, c.SemanticModel)
&& !Language.Syntax.IsInExpressionTree(c.SemanticModel, invocation))
{
c.ReportIssue(Diagnostic.Create(Rule, Language.Syntax.NodeIdentifier(invocation)?.GetLocation()));
var message = c.SemanticModel.GetTypeInfo(left).Type.DerivesFrom(KnownType.System_Array) ? ArrayMessage : GenericMessage;
c.ReportIssue(Diagnostic.Create(Rule, Language.Syntax.NodeIdentifier(invocation)?.GetLocation(), message));

Choose a reason for hiding this comment

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

What do you think about refactoring IsCorrectType to this?

    private static bool IsCorrectType(SyntaxNode left, SemanticModel model, out bool isArray)
    {
        var type = model.GetTypeInfo(left).Type;
        isArray = type.DerivesFrom(KnownType.System_Array);
        return isArray || type.DerivesFromAny(ListTypes);
    }

This way you do the semantic check only once, and then use the boolean out parameter for your message ternary:

var message = isArray ? ArrayMessage : GenericMessage;

Copy link
Member Author

Choose a reason for hiding this comment

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

While your suggestion is more performant, I prefer to use the out parameters only if needed. The second call should be rather fast due to caching.

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like the out parameter won 😱

Choose a reason for hiding this comment

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

😱

Copy link

sonarcloud bot commented Mar 19, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

Copy link

sonarcloud bot commented Mar 19, 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

@costin-zaharia-sonarsource costin-zaharia-sonarsource merged commit 36cb7bd into master Mar 20, 2024
32 checks passed
@costin-zaharia-sonarsource costin-zaharia-sonarsource deleted the costin/S6602-message branch March 20, 2024 12:39
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.

Rule S6602: Improve the logging message when recommending for Array.Find
2 participants