-
Notifications
You must be signed in to change notification settings - Fork 465
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
Added support for StringSyntaxAttribute.CompositeFormat in CA2241 (#6… #6272
Added support for StringSyntaxAttribute.CompositeFormat in CA2241 (#6… #6272
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #6272 +/- ##
========================================
Coverage 96.38% 96.39%
========================================
Files 1379 1379
Lines 322290 322497 +207
Branches 10461 10465 +4
========================================
+ Hits 310652 310859 +207
+ Misses 9144 9141 -3
- Partials 2494 2497 +3 |
...Tests/Microsoft.NetCore.Analyzers/Runtime/ProvideCorrectArgumentsToFormattingMethodsTests.cs
Show resolved
Hide resolved
Thanks for working on this! @mavasani, does this approach make sense / is this something we do in other analyzers? My concern would be memory consumption in large projects; is that something we find is an issue? |
...yzers/Core/Microsoft.NetCore.Analyzers/Runtime/ProvideCorrectArgumentsToFormattingMethods.cs
Outdated
Show resolved
Hide resolved
...yzers/Core/Microsoft.NetCore.Analyzers/Runtime/ProvideCorrectArgumentsToFormattingMethods.cs
Show resolved
Hide resolved
...yzers/Core/Microsoft.NetCore.Analyzers/Runtime/ProvideCorrectArgumentsToFormattingMethods.cs
Outdated
Show resolved
Hide resolved
...Tests/Microsoft.NetCore.Analyzers/Runtime/ProvideCorrectArgumentsToFormattingMethodsTests.cs
Outdated
Show resolved
Hide resolved
...yzers/Core/Microsoft.NetCore.Analyzers/Runtime/ProvideCorrectArgumentsToFormattingMethods.cs
Outdated
Show resolved
Hide resolved
...yzers/Core/Microsoft.NetCore.Analyzers/Runtime/ProvideCorrectArgumentsToFormattingMethods.cs
Outdated
Show resolved
Hide resolved
You might want to use https://github.com/dotnet/roslyn-analyzers/blob/main/src/Utilities/Compiler/PooledObjects/PooledConcurrentDictionary.cs instead.
Saving each invoked method doesn't feel right, but I'll defer to @mavasani |
We actually use this approach in other analyzers already, so looks it is OK. @manfred-brands please resolve merge conflict and other comments. Thank you! |
This way we only need to check every method definition once.
511d475
to
ce87a5b
Compare
Branch rebased and code-review changes applied. |
57ada20
to
4f0bcdd
Compare
...yzers/Core/Microsoft.NetCore.Analyzers/Runtime/ProvideCorrectArgumentsToFormattingMethods.cs
Outdated
Show resolved
Hide resolved
…ideCorrectArgumentsToFormattingMethods.cs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you @manfred-brands!
@stephentoub @manfred-brands what would be the expected behavior for methods like
The change in this PR is what cause the problem mentioned in dotnet/runtime#90357 |
@tarekgh First, an analyzer should never throw exceptions. What it should do with a method that not actually formats is another question. The error is in pre-exiting code where the next parameter is checked. It is triggered by this PR because previously only a few methods were checked and now this method is added to be checked. |
The intent of the issue was this: In other words, validate that the attributed composite format string itself was formatted correctly (eg no dangling braces), not that the were a number of additional arguments passed to the same method to satisfy the format items in that string. |
@stephentoub That is not what the analyzer does nor what is documented in CA2241. It clearly states it checks the additional parameters.
|
@stephentoub If you create a bug issue to account for the no-params case, I will create a PR. |
The analyzer flags public static CompositeFormat Parse([StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format) usage it could behave similarly for
Could you reference the original issue dotnet/runtime#90357 for that? |
It does. It also checks whether the right number of arguments is supplied to match the holes, which is why the issue calls out that part isn't relevant. But, for example, all of these trigger CA2241: string.Format("{");
string.Format("{0}}", 42);
string.Format("{0,}", 42); because the string is malformed. |
Unfortunately, it doesn't actual say that the format string is malformed. The class is called I created #6884 to prevent the IndexOutOfRangeException. To keep with the spirit of @stephentoub to only check the format, I also created #6885. |
I prefer going with #6885 and we can decide if we can introduce a new diagnostic id for checking the specification string case. |
Thank you, I agree we need to determine if we should use different message and/or different ID. I can create a separate issue for that. As #6885 fix the Please let us know what you think @stephentoub @jeffhandley |
Small clarification, The change of behavior when having |
Thank you for explanation I haven't had checked the PR yet, now I see that.
This change definitely needs a message change at least, showing With that being said now I am OK with accepting the fix in #6885 as it makes the behavior of |
@manfred-brands is it easy to change the messages for the scenario like |
The IDE0043 issue seems to have been resolved as to retire IDE0043. |
I have made the changes, but not sure who will make the translations for the .resx files. I also found a "bug" in the rule where it only counted the number of unique references, not taking the actual index into account. The below didn't raise an error:
But this one did:
Although the latter is debatable, it it valid. |
Good catch thanks!
Thank you! No need to worry about translations, a language experts would do that later
Seems this rule needs a lot of fixes, FYI @mavasani |
I prefer going with #6885 here too. I'll review that PR with the expectation of getting it approved for merge into .NET 8 RC2. |
Fixes #6012
@stephentoub The existing implementation uses an ImmutableDictionary for the pre-defined methods.
This means that for all other methods, if called multiple times, the check if the methods is a format method is re-done every invocation.
I added one extra commit to use a ConcurrentDictionary instead which is updated when a method is discovered so the next time this method is called it is found immediately.
I don't know if this is safe. The class is created as part of a
RegisterCompilationStartAction
so isolated per compilation.One other optimization is to also add methods to the dictionary that have been checked, but not found to be format methods.
This way we only need to check every method definition once.