Skip to content

Commit

Permalink
Disable MA0141/MA0142/MA0148/MA0149 in System.Linq.Expression
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Jan 27, 2024
1 parent 21e8a55 commit 8e82029
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,43 @@ public override void Initialize(AnalysisContext context)
if (tree.GetCSharpLanguageVersion() < Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8)
return;

context.RegisterOperationAction(AnalyzeBinary, OperationKind.Binary);
var analyzerContext = new AnalyzerContext(context.Compilation);
context.RegisterOperationAction(analyzerContext.AnalyzeBinary, OperationKind.Binary);
});
}

private static void AnalyzeBinary(OperationAnalysisContext context)
private sealed class AnalyzerContext(Compilation compilation)
{
var operation = (IBinaryOperation)context.Operation;
if (operation is { OperatorKind: BinaryOperatorKind.Equals or BinaryOperatorKind.NotEquals, OperatorMethod: null })
private readonly OperationUtilities _operationUtilities = new(compilation);

public void AnalyzeBinary(OperationAnalysisContext context)
{
var leftIsNull = UsePatternMatchingForEqualityComparisonsCommon.IsNull(operation.LeftOperand);
var rightIsNull = UsePatternMatchingForEqualityComparisonsCommon.IsNull(operation.RightOperand);
if (leftIsNull ^ rightIsNull)
{
context.ReportDiagnostic(operation.OperatorKind is BinaryOperatorKind.Equals ? RuleEqualNull : RuleNotEqualNull, operation);
}
else if (!leftIsNull && !rightIsNull)
var operation = (IBinaryOperation)context.Operation;
if (operation is { OperatorKind: BinaryOperatorKind.Equals or BinaryOperatorKind.NotEquals, OperatorMethod: null })
{
var leftIsConstant = UsePatternMatchingForEqualityComparisonsCommon.IsConstantLiteral(operation.LeftOperand);
var rightIsConstant = UsePatternMatchingForEqualityComparisonsCommon.IsConstantLiteral(operation.RightOperand);
if (leftIsConstant ^ rightIsConstant)
var leftIsNull = UsePatternMatchingForEqualityComparisonsCommon.IsNull(operation.LeftOperand);
var rightIsNull = UsePatternMatchingForEqualityComparisonsCommon.IsNull(operation.RightOperand);
if (leftIsNull ^ rightIsNull)
{
if (_operationUtilities.IsInExpressionContext(operation))
return;

context.ReportDiagnostic(operation.OperatorKind is BinaryOperatorKind.Equals ? RuleEqualNull : RuleNotEqualNull, operation);
}
else if (!leftIsNull && !rightIsNull)
{
context.ReportDiagnostic(operation.OperatorKind is BinaryOperatorKind.Equals ? RuleEqualConstant : RuleNotEqualConstant, operation);
var leftIsConstant = UsePatternMatchingForEqualityComparisonsCommon.IsConstantLiteral(operation.LeftOperand);
var rightIsConstant = UsePatternMatchingForEqualityComparisonsCommon.IsConstantLiteral(operation.RightOperand);
if (leftIsConstant ^ rightIsConstant)
{
if (_operationUtilities.IsInExpressionContext(operation))
return;

context.ReportDiagnostic(operation.OperatorKind is BinaryOperatorKind.Equals ? RuleEqualConstant : RuleNotEqualConstant, operation);
}
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ private static ProjectBuilder CreateProjectBuilder()
.WithCodeFixProvider<UsePatternMatchingForEqualityComparisonsFixer>();
}

[Fact]
public async Task DisabledInExpression()
{
await CreateProjectBuilder()
.WithSourceCode("""
using System;
using System.Linq;
using System.Linq.Expressions;
_ = (Expression<Func<int, bool>>)(item => item == 0);
""")
.ValidateAsync();
}

[Fact]
public async Task NullCheckForNullableOfT()
{
Expand Down

0 comments on commit 8e82029

Please sign in to comment.