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

Ensure ForwardCancellationTokenAnalyzer treats usages of default expressions properly #6617

Merged
merged 3 commits into from Dec 2, 2022

Conversation

DavidBoike
Copy link
Member

@DavidBoike DavidBoike commented Nov 30, 2022

Backports the following to the release-8.0 branch.


This fixes a bug discovered in the ForwardCancellationTokenAnalyzer and adds several more test cases.

The analyzer is supposed to ignore when you pass CancellationToken.None, but throwing an exception when doing the same thing but by using a default operator or literal which were introduced between C# 7 and 8:

// OK
await Task.Delay(100, CancellationToken.None);

// Throwing exceptions in 8.0.0
await Task.Delay(100, default(CancellationToken));
await Task.Delay(100, default);

This requires better analysis to make sure the presence of default doesn't throw it off:

public async Task SomeMethod(int a, string b, CancellationToken token = default) { }

// Would pass the 0 and null but not provide a token:
await SomeMethod(default, default);

This PR also introduces several test cases around the use of await foreach… when using async enumerables.


Symptoms

During development, the following compiler warning is observed:

AD0001: Analyzer 'NServiceBus.Core.Analyzer.ForwardCancellationTokenAnalyzer' threw an exception of type 'System.IndexOutOfRangeException' with message 'Index was outside the bounds of the array.'

Who's affected

Anyone using NServiceBus 8.0.0 attempting to analyze code that passes default(CancellationToken) or default for a CancellationToken parameter is affected.

Root cause

The root cause is a bug in an NServiceBus analyzer which was not expecting one of the default expressions to be used.

@DavidBoike DavidBoike added this to the 8.0.1 milestone Dec 2, 2022
@DavidBoike DavidBoike merged commit 9a0fd52 into release-8.0 Dec 2, 2022
@DavidBoike DavidBoike deleted the analyzer-cases-8 branch December 2, 2022 18:49
@DavidBoike DavidBoike added the Bug label Dec 2, 2022
@DavidBoike DavidBoike changed the title Ensure ForwardCancellationTokenAnalyzer treats usages of default expressions properly (release-8.0) Ensure ForwardCancellationTokenAnalyzer treats usages of default expressions properly Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants