Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public static bool IsCSharp13OrAbove(this LanguageVersion languageVersion)
return languageVersion >= (LanguageVersion)1300;
}

public static bool IsCSharp14OrAbove(this LanguageVersion languageVersion)
{
return languageVersion >= (LanguageVersion)1400;
}

public static bool IsCSharp10OrBelow(this LanguageVersion languageVersion)
{
return languageVersion <= (LanguageVersion)1000;
Expand Down
2 changes: 1 addition & 1 deletion src/Meziantou.Analyzer/Rules/NamedParameterAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ bool IsParams(SyntaxNode node)
if (invokedMethodSymbol.Name.StartsWith("With", StringComparison.Ordinal) && invokedMethodSymbol.ContainingType.IsOrInheritFrom(syntaxNodeType))
return;

if (operation is not null && operationUtilities.IsInExpressionContext(operation))
if (operation is not null && !operation.GetCSharpLanguageVersion().IsCSharp14OrAbove() && operationUtilities.IsInExpressionContext(operation))
return;

if (syntaxContext.Options.TryGetConfigurationValue(expression.SyntaxTree, RuleIdentifiers.UseNamedParameter + ".excluded_methods_regex", out var excludedMethodsRegex))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,47 +597,77 @@ await CreateProjectBuilder()
}

[Fact]
public async Task Expression_ShouldNotReportDiagnostic()
public async Task Expression_IEnumerable_ShouldNotReportDiagnostic()
{

await CreateProjectBuilder()
.WithSourceCode(@"
using System.Linq;
using System.Collections.Generic;
.WithSourceCode("""
using System.Linq;
using System.Collections.Generic;

class Test
{
public Test()
{
IEnumerable<string> query = null;
query.Where(x => M([||]false));
}
class Test
{
public Test()
{
IEnumerable<string> query = null;
query.Where(x => M([||]false));
}

static bool M(bool a) => false;
}
")
static bool M(bool a) => false;
}

""")
.ValidateAsync();
}

await CreateProjectBuilder()
.WithSourceCode(@"using System.Linq;
class Test
{
public Test()
[Fact]
public async Task Expression_IQueryable_ShouldNotReportDiagnostic()
{
IQueryable<string> query = null;
query.Where(x => M(false));
await CreateProjectBuilder()
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8)
.WithSourceCode("""
using System.Linq;
class Test
{
public Test()
{
IQueryable<string> query = null;
query.Where(x => M(false));
}

static bool M(bool a) => false;
}
""")
.ValidateAsync();
}

static bool M(bool a) => false;
}
")
#if CSHARP14_OR_GREATER
[Fact]
public async Task Expression_ParamsInLambda_ShouldNotReportDiagnostic()
{
await CreateProjectBuilder()
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp14)
.WithSourceCode("""
using System.Linq;
class Test
{
public Test()
{
IQueryable<string> query = null;
query.Where(x => M([|false|]));
}

static bool M(bool a) => false;
}
""")
.ValidateAsync();
}
#endif

[Fact]
public async Task Expression_ShouldNotReportDiagnostic2()
{
await CreateProjectBuilder()
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8)
.WithSourceCode(@"
using System;
using System.Linq;
Expand Down