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

Handle switching from params to non-params overload in xUnit1051 #192

Merged
merged 3 commits into from
Dec 1, 2024

Conversation

SapiensAnatis
Copy link
Contributor

@SapiensAnatis SapiensAnatis commented Nov 29, 2024

If the analyzer has fired on a params method, it means that the new overload is actually array + CancellationToken, since C# forbids placing any argument after a params argument. To avoid generating invalid code, we need to replace the params expression with an array creation expression.

Does not currently handle the expanded range of allowed params types implemented in C# 13. Considering you can use any type that implements IEnumerable<T>, I think it would be quite difficult to handle all the different cases there. Being a new feature, it's also less likely to be used, though this will of course change over time...

Closes #3068

@SapiensAnatis
Copy link
Contributor Author

@dotnet-policy-service agree

If the analyzer has fired on a `params` method, it means that the new
overload is actually array + `CancellationToken`, since C# forbids
placing any argument after a params argument. To avoid generating
invalid code, we need to replace the params expression with an array
creation expression.

Does not currently handle the expanded range of allowed `params` types
implemented in C# 13. Considering you can use _any_ type that implements
`IEnumerable<T>`, I think it would be quite difficult to handle all the
different cases there.
Being a new feature, it's also less likely to be used, though this will
of course change over time...

Closes [#3068](xunit/xunit#3068)
@bradwilson
Copy link
Member

It looks like there are some trivial test failures to be fixed.

Normally I would fix these, but your branch is not open for me to contribute to. 😄

@SapiensAnatis
Copy link
Contributor Author

SapiensAnatis commented Nov 30, 2024

Oops! That should hopefully be fixed now. Thanks for letting me know.

It's odd you can't commit to my branch, that setting appears to be enabled 😕

image

edit: on the whitespace issue, I've noticed it's because the raw string literals use spaces for indentation but the rest of the code uses tabs. I've used tabs in my raw string literals, would you prefer these be switched to spaces?

@bradwilson
Copy link
Member

edit: on the whitespace issue, I've noticed it's because the raw string literals use spaces for indentation but the rest of the code uses tabs. I've used tabs in my raw string literals, would you prefer these be switched to spaces?

I've tended to use spaces, because the way the fixers work by default in testing is the they appear to use the default settings (i.e., 4 spaces), so matching that default makes the results much more predictable.

Unfortunately, it's not easy to switch between tabs and spaces when editing the multi-line literal strings, which means I'm wearing out my spacebar. 😂

@SapiensAnatis
Copy link
Contributor Author

Makes sense. For consistency's sake I've adapted the new tests to do the same. Sorry you keep having to approve the workflows!

I assume the 4 space issue appears when a code fix adds a new line? Maybe you could fix it by passing an .editorconfig file that specifies to use tabs into the test? Looks like you could do this in VerifyCodeFixV3 by setting test.TestState.AnalyzerConfigFiles: https://github.com/dotnet/roslyn-sdk/blob/35d5e46fd5c403194692c645d912a17d36ed74f5/tests/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing.UnitTests/AnalyzerConfigFilesTests.cs#L38-L41

@bradwilson
Copy link
Member

Maybe you could fix it by passing an .editorconfig file

I don't think I knew you could do this. 😂 I might do this and then fix everything to use tabs! Thanks!

@bradwilson bradwilson merged commit 6897a29 into xunit:main Dec 1, 2024
5 checks passed
@bradwilson
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

xUnit1051 code fix chooses incorrect overload for CancellationToken
2 participants