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

MA0003 excludes Expression.Constant #605

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/Meziantou.Analyzer/Rules/NamedParameterAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -53,6 +54,7 @@ public override void Initialize(AnalysisContext context)
var keyValuePairTokenType = compilationContext.Compilation.GetBestTypeByMetadataName("System.Collection.Generic.KeyValuePair`2");
var propertyBuilderType = compilationContext.Compilation.GetBestTypeByMetadataName("Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder`1");
var syntaxNodeType = compilationContext.Compilation.GetBestTypeByMetadataName("Microsoft.CodeAnalysis.SyntaxNode");
var expressionType = compilationContext.Compilation.GetBestTypeByMetadataName("System.Linq.Expressions.Expression");
var operationUtilities = new OperationUtilities(compilationContext.Compilation);

compilationContext.RegisterSyntaxNodeAction(syntaxContext =>
Expand Down Expand Up @@ -222,6 +224,9 @@ bool IsParams(SyntaxNode node)
if (IsMethod(invokedMethodSymbol, xunitAssertTokenType, "*"))
return;

if (IsMethod(invokedMethodSymbol, expressionType, nameof(Expression.Constant)))
return;

if ((string.Equals(invokedMethodSymbol.Name, "Parse", StringComparison.Ordinal) || string.Equals(invokedMethodSymbol.Name, "TryParse", StringComparison.Ordinal)) && argumentIndex == 0)
return;

Expand Down
16 changes: 16 additions & 0 deletions tests/Meziantou.Analyzer.Test/Rules/NamedParameterAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,22 @@ void A()
a.SetResult(null);
}
}
")
.ValidateAsync();
}

[Fact]
public async Task Expression_Constant()
{
await CreateProjectBuilder()
.WithSourceCode(@"
class Test
{
void A()
{
_ = System.Linq.Expressions.Expression.Constant(null);
}
}
")
.ValidateAsync();
}
Expand Down