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 @@ -37,5 +37,24 @@ public async Task TestPrimaryRecordConstructorIncludeMissingParametersAsync(stri

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3977, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3977")]
public async Task TestLambdaDiscardParametersAsync()
{
var testCode = @"
/// <summary>Test class.</summary>
public class TestClass
{
/// <summary>Test method.</summary>
public void TestMethod()
{
System.Func<int, int, int> handler = (_, _) => 0;
}
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@

namespace StyleCop.Analyzers.Test.CSharp9.DocumentationRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.DocumentationRules;
using Xunit;

public partial class SA1612CSharp9UnitTests : SA1612CSharp8UnitTests
{
[Fact]
[WorkItem(3977, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3977")]
public async Task TestLambdaDiscardParametersAsync()
{
var testCode = @"
/// <summary>Test class.</summary>
public class TestClass
{
/// <summary>Test method.</summary>
public void TestMethod()
{
System.Func<int, int, int> handler = (_, _) => 0;
}
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,34 @@

namespace StyleCop.Analyzers.Test.CSharp9.NamingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.NamingRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.NamingRules.SA1312VariableNamesMustBeginWithLowerCaseLetter,
StyleCop.Analyzers.NamingRules.RenameToLowerCaseCodeFixProvider>;

public partial class SA1312CSharp9UnitTests : SA1312CSharp8UnitTests
{
[Fact]
[WorkItem(3977, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3977")]
public async Task TestLambdaDiscardParametersDoNotReportAsync()
{
var testCode = @"
using System;
public class TestClass
{
public void Test()
{
Func<int, int, int> handler = (_, _) => 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Irrelevant test, since "handler" is the only variable here?

Copy link
Member Author

@sharwell sharwell Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a verification that _ is still ignored when it's treated as a true discard by the compiler. I was torn on whether to keep this test or not considering SA1313 is the one for parameters.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My assumption was that this diagnostic would only analyze "handler", not the discards, but that might be wrong

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly the correct assumption. The question is, considering we can't cover all possible scenarios in all possible cases, where do we draw the line on tests to include for "unrelated items"? This one seems pretty close to the not-worth-it line, but ... 🤷

}
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ namespace StyleCop.Analyzers.Test.CSharp9.NamingRules

public partial class SA1313CSharp9UnitTests : SA1313CSharp8UnitTests
{
[Fact]
[WorkItem(3977, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3977")]
public async Task TestLambdaDiscardParametersAsync()
{
var testCode = @"
using System;

public class TestClass
{
public void Test()
{
Action<int, int> handler = (_, _) => { };
}
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3168, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3168")]
[WorkItem(3181, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3181")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.DocumentationRules
{
using System.Collections.Generic;
Expand Down Expand Up @@ -562,35 +560,13 @@ public class ClassName
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

private static DiagnosticResult[] GetExpectedDiagnostics(DiagnosticResult normallyExpected, string declaration)
{
return GetExpectedDiagnostics(new[] { normallyExpected }, declaration);
}

// Syntax node actions for type declarations with a primary constructor were called twice
// before support for c# 11 was added.
private static DiagnosticResult[] GetExpectedDiagnostics(DiagnosticResult[] normallyExpected, string declaration)
{
var isPrimaryConstructor = declaration.Contains("record") || declaration.Contains("class") || declaration.Contains("struct");

if (isPrimaryConstructor && !LightupHelpers.SupportsCSharp11)
{
// Diagnostic issued twice because of https://github.com/dotnet/roslyn/issues/53136 and https://github.com/dotnet/roslyn/issues/70488
return normallyExpected.Concat(normallyExpected).ToArray();
}
else
{
return normallyExpected;
}
}

private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
protected static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, expected, ignoreCompilerDiagnostics: false, cancellationToken);

private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, CancellationToken cancellationToken)
protected static Task VerifyCSharpDiagnosticAsync(string source, string? testSettings, DiagnosticResult[] expected, CancellationToken cancellationToken)
=> VerifyCSharpDiagnosticAsync(source, testSettings, expected, ignoreCompilerDiagnostics: false, cancellationToken);

private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, bool ignoreCompilerDiagnostics, CancellationToken cancellationToken)
protected static Task VerifyCSharpDiagnosticAsync(string source, string? testSettings, DiagnosticResult[] expected, bool ignoreCompilerDiagnostics, CancellationToken cancellationToken)
{
string contentWithoutParamDocumentation = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<ClassName>
Expand Down Expand Up @@ -696,5 +672,27 @@ private static Task VerifyCSharpDiagnosticAsync(string source, string testSettin
test.ExpectedDiagnostics.AddRange(expected);
return test.RunAsync(cancellationToken);
}

private static DiagnosticResult[] GetExpectedDiagnostics(DiagnosticResult normallyExpected, string declaration)
{
return GetExpectedDiagnostics(new[] { normallyExpected }, declaration);
}

// Syntax node actions for type declarations with a primary constructor were called twice
// before support for c# 11 was added.
private static DiagnosticResult[] GetExpectedDiagnostics(DiagnosticResult[] normallyExpected, string declaration)
{
var isPrimaryConstructor = declaration.Contains("record") || declaration.Contains("class") || declaration.Contains("struct");

if (isPrimaryConstructor && !LightupHelpers.SupportsCSharp11)
{
// Diagnostic issued twice because of https://github.com/dotnet/roslyn/issues/53136 and https://github.com/dotnet/roslyn/issues/70488
return normallyExpected.Concat(normallyExpected).ToArray();
}
else
{
return normallyExpected;
}
}
}
}
Loading