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
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix analyzer [RCS1265](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1265) to not report catch clauses with a `when` filter ([PR](https://github.com/dotnet/roslynator/pull/1789))
- Fix analyzer [RCS0034](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0034) for types with a primary constructor and multiple constraint clauses ([PR](https://github.com/dotnet/roslynator/pull/1791))
- [CLI] Fix GitLab output format to use relative paths, forward slashes, and 1-based line numbers ([PR](https://github.com/dotnet/roslynator/pull/1792))
- Fix analyzer [RCS1231](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1231) to not report `CancellationToken` in sync methods returning `Task` ([PR](https://github.com/dotnet/roslynator/pull/1802))

## [4.16.0] - 2026-08-08

Expand Down
6 changes: 6 additions & 0 deletions src/Analyzers/CSharp/Analysis/RefReadOnlyParameterAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ private static void Analyze(
continue;
}

if (type.HasMetadataName(in MetadataNames.System_Threading_CancellationToken)
&& methodSymbol.ReturnType.IsWellKnownTaskType())
{
continue;
}

if (parameter.RefKind != RefKind.None)
continue;

Expand Down
43 changes: 43 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1231MakeParameterRefReadOnlyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,49 @@ public void M(DateTime dt)

public DateTime P { get; set; }
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.MakeParameterRefReadOnly)]
public async Task TestNoDiagnostic_CancellationToken_SyncTaskReturningMethod()
{
await VerifyNoDiagnosticAsync(@"
using System.Threading;
using System.Threading.Tasks;

class C
{
public Task DoThatWay(CancellationToken cancellationToken)
{
return Task.FromCanceled(cancellationToken);
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.MakeParameterRefReadOnly)]
public async Task Test_ReadOnlyStruct_SyncTaskReturningMethod()
{
await VerifyDiagnosticAndFixAsync(@"
using System.Threading.Tasks;

readonly struct C
{
public Task<int> M(C [|c|])
{
return Task.FromResult(c.GetHashCode());
}
}
", @"
using System.Threading.Tasks;

readonly struct C
{
public Task<int> M(in C c)
{
return Task.FromResult(c.GetHashCode());
}
}
");
}
}
Loading