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

S2674 Cleanup for SonarWay promotion #9609

Merged
merged 1 commit into from
Aug 7, 2024
Merged
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
72 changes: 36 additions & 36 deletions analyzers/src/SonarAnalyzer.CSharp/Rules/StreamReadStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,45 @@

using System.IO;

namespace SonarAnalyzer.Rules.CSharp
namespace SonarAnalyzer.Rules.CSharp;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class StreamReadStatement : SonarDiagnosticAnalyzer
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class StreamReadStatement : SonarDiagnosticAnalyzer
{
private const string DiagnosticId = "S2674";
private const string MessageFormat = "Check the return value of the '{0}' call to see how many bytes were read.";
private const string DiagnosticId = "S2674";
private const string MessageFormat = "Check the return value of the '{0}' call to see how many bytes were read.";

private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, MessageFormat);
private static readonly ISet<string> ReadMethodNames = new HashSet<string>
{
nameof(Stream.Read),
nameof(Stream.ReadAsync),
"ReadAtLeast", // Net7: https://learn.microsoft.com/dotnet/api/system.io.stream.readatleast#applies-to
"ReadAtLeastAsync",
};
private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, MessageFormat);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
private static readonly ISet<string> ReadMethodNames = new HashSet<string>(StringComparer.Ordinal)
{
nameof(Stream.Read),
nameof(Stream.ReadAsync),
"ReadAtLeast", // Net7: https://learn.microsoft.com/dotnet/api/system.io.stream.readatleast#applies-to
"ReadAtLeastAsync",
};

protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(c =>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);

protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(c =>
{
var statement = (ExpressionStatementSyntax)c.Node;
var expression = statement.Expression switch
{
AwaitExpressionSyntax awaitExpression => awaitExpression.AwaitedExpressionWithoutConfigureAwait(),
var x => x,
};
expression = expression.RemoveConditionalAccess();
if (expression is InvocationExpressionSyntax invocation
&& invocation.GetMethodCallIdentifier() is { } methodIdentifier
&& ReadMethodNames.Contains(methodIdentifier.Text)
&& c.SemanticModel.GetSymbolInfo(expression).Symbol is IMethodSymbol method
&& (method.ContainingType.Is(KnownType.System_IO_Stream)
|| (method.IsOverride && method.ContainingType.DerivesOrImplements(KnownType.System_IO_Stream))))
{
var statement = (ExpressionStatementSyntax)c.Node;
var expression = statement.Expression switch
{
AwaitExpressionSyntax awaitExpression => awaitExpression.AwaitedExpressionWithoutConfigureAwait(),
var x => x,
};
expression = expression.RemoveConditionalAccess();
if (expression is InvocationExpressionSyntax invocation
&& invocation.GetMethodCallIdentifier() is { } methodIdentifier
&& ReadMethodNames.Contains(methodIdentifier.Text, StringComparer.Ordinal)
&& c.SemanticModel.GetSymbolInfo(expression).Symbol is IMethodSymbol method
&& (method.ContainingType.Is(KnownType.System_IO_Stream)
|| (method.IsOverride && method.ContainingType.DerivesOrImplements(KnownType.System_IO_Stream))))
{
c.ReportIssue(Rule, expression, method.Name);
}
},
SyntaxKind.ExpressionStatement);
}
c.ReportIssue(Rule, expression, method.Name);
}
},
SyntaxKind.ExpressionStatement);
}
Loading