Skip to content

Commit

Permalink
Update reporting messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Sep 25, 2024
1 parent 4e3d581 commit 9fe57b3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ class Noncompliant
{
void LeftIsNullable(decimal? left)
{
var result = left + 42m; // Noncompliant
var result = left + 42m; // Noncompliant {{Value of operand is potentially null.}}
// ^^^^
}

void RightIsNullable(decimal? right)
{
var result = 42 + right; // Noncompliant
var result = 42 + right; // Noncompliant {{Value of operand is potentially null.}}
// ^^^^^
}

void BothAreNullable(decimal? left, decimal? right)
{
var result = left + right; // Noncompliant
var result = left + right; // Noncompliant {{Result of operation is potentially null.}}
// ^^^^^^^^^^^^
}

Expand Down
2 changes: 1 addition & 1 deletion src/Qowaiv.CodeAnalysis.CSharp/Rule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static partial class Rule
public static DiagnosticDescriptor ApplyArithmeticOperationsOnNonNullablesOnly => New(
id: 17,
title: "Apply arithmetic operations on non-nullables only",
message: "This arithmetic operation potentially has null result.",
message: "{0} is potentially null.",
description:
".NET allows arithmetic operations between nullable value types. The " +
"outcome will be null if any of the arguments turns out to be null, " +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

namespace Qowaiv.CodeAnalysis.Rules;
namespace Qowaiv.CodeAnalysis.Rules;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class ApplyArithmeticOperationsOnNonNullablesOnly() : CodingRule(Rule.ApplyArithmeticOperationsOnNonNullablesOnly)
Expand All @@ -23,29 +22,28 @@ protected override void Register(AnalysisContext context)

private void Report(SyntaxNodeAnalysisContext context)
{
if (context.Node is BinaryExpressionSyntax binary
&& Nullable(binary.Left,binary.Right) is { } nullBinary)
if (context.Node is BinaryExpressionSyntax binary)
{
context.ReportDiagnostic(Diagnostic, nullBinary);
Report(context, binary.Left, binary.Right);
}
else if (context.Node is AssignmentExpressionSyntax assign
&& Nullable(assign.Left, assign.Right) is { } nullAssign)
else if (context.Node is AssignmentExpressionSyntax assign)
{
context.ReportDiagnostic(Diagnostic, nullAssign);
Report(context, assign.Left, assign.Right);
}
}

SyntaxNode? Nullable(ExpressionSyntax left, ExpressionSyntax right)
{
var l = IsNullable(left);
var r = IsNullable(right);
private void Report(SyntaxNodeAnalysisContext context, ExpressionSyntax left, ExpressionSyntax right)
{
var l = IsNullable(left);
var r = IsNullable(right);

return context switch
{
_ when l && r => context.Node,
_ when l => left,
_ when r => right,
_ => null,
};
if (l && r)
{
context.ReportDiagnostic(Diagnostic, context.Node, "Result of operation");
}
else if (l || r)
{
context.ReportDiagnostic(Diagnostic, l ? left : right, "Value of operand");
}

bool IsNullable(ExpressionSyntax expression)
Expand Down

0 comments on commit 9fe57b3

Please sign in to comment.