-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
IDE0028/IDE0090/IDE00300/IDE0305 do not catch some expressions #72699
Comments
@mavasani, did you move this because IDE rules belong here and CA rules belong in roslyn-analyzers? |
@bzd3y Yes, IDE rules belong in dotnet/roslyn. |
In regards to these items:
These seem like a better fit for "Simplify collection initialization" than for "Use collection expression". Each of these can be simplified even without using collection expressions. The code fix for these could vary based on whether collection expressions are supported/preferred by the project. |
@sharwell okay, thanks. I wasn't clear on that but now it makes sense. As for "simplify collection initialization", I agree but I tried to stick to what gets triggered without something like a wrapping expression that seems to hide the current rules that are triggered without it. Which rule specifically are you talking about? IDE0028? Several of them involve simplifying collection initializations. |
There's no hiding of rules. It's just a question of how far we took the analysis and how many patterns we want to invest in detecting and suggesting updates for. |
@CyrusNajmabadi I understand that and didn't mean deliberate hiding, but inadvertent hiding in terms of the final result. These are collection initializations that could be simplified, but that gets hidden in cases like them being passed into a method call or apparently if the initialization doesn't occur on the same line as the declaration. I believe this is all the same pattern here, or at least fits in with at least one of the patterns handled by the rules that I listed. |
So, again, the terminology isn't correct here. THere's no hiding (inadvertent or otherwise) :). These systems support the set of cases we've explicitly added support for. If these cases are desired, it's not a matter of unhiding anything. It's a matter of just adding support for the additional patterns suggested :)
These are all distinct patterns. The analyzer would need to be updated to look for an support these. |
Markign up for grabs. We'd likely take reasonable enhancements here to the patterns detected. |
@CyrusNajmabadi You are taking "hiding" too literally and misunderstanding my point, as if I am accusing you of something. I'm not sure of the use in arguing the semantics of something being hidden. But, fine, there is no hiding. Nothing gets hidden. As for this being the same pattern, the pattern I am talking about is the exact same pattern as at least one of the patterns listed. Consider: List<string> list = new List<string>();
List<string> list2 = new List<string>().ToList(); Those can have the exact same patterns applied to them, except for the second line, which also has a suggestion for rule IDE0305. But only the first line suggests anything other than IDE0305. It suggests IDE0028 and IDE0090. And both of those could be applied in exactly the same way, with exactly the same refactor, to the second line. And if these were arrays instead of In short, both involve at least If you want to make it a new rule for some reason, then that is fine. The only reason I am "arguing" this is because you seem to be misunderstanding something, so I am trying to clear it up. It seems to me that the fix or enhancement here is that IDE0028, IDE0300 and IDE0090 could trigger off of an initialization that is being wrapped by another expression and beyond the first level of depth. I haven't worked with analyzers that much, but isn't there a visitor pattern available that would be able to hit these types of expressions at any depth? |
This has less to do with you and more to do with the current project. We use 'hiding' in this solution many places to have a very specific meaning, and users who work on this project are likely to interpret this term in the context of this project even if that is not the intended meaning. To avoid misinterpretation by other users who participate in this issue, Cyrus called this point out and explicitly clarified that this issue does not relate to 'hiding' in the sense that we normally use it. |
This is not what I'm suggesting. What I said before is simply:
And
|
Yes. That's exactly what I was saying with:
:-) Much of this seems reasonable. As I mentioned, we would very likely take community contributions here to support more cases. |
@sharwell Okay, I didn't realize that, but it's good to know. Thank you for explaining it. @CyrusNajmabadi Sorry for the confusion. |
This cannot trigger anything. |
I think I added that one because I was thinking it could be simplified to But even so, that redundancy was the reason for proposing a new rule (could be in one of these analyzers) to trigger on the redundant copies like |
Analyzer
Diagnostic ID: IDE0028:
Use collection initializers or expressions
IDE0090:
Simplify new expression
IDE0300:
Use collection expression for array
IDE0305:
Use collection expression for fluent
Analyzer source
SDK: Built-in CA analyzers in .NET 8 SDK or later
Version: SDK 8.0.200
OR
NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers
Version: 8.0.0
Describe the bug
I suppose this might not be considered a bug and could be more of a suggestion. Code like the following does not seem to trigger any of the collection expression/simplification rules.
Steps To Reproduce
A reproduction project can be found here: https://github.com/bzd3y/ReproductionProject . Look at the Analyzers project Error List. It may help to filter the Error List to "Current Project".
Expected behavior
The analyzers should be triggered for the above code. The expected behavior is documented there and in the reproduction project the code came from.
Actual behavior
Not all of the relevant analyzers seem like they are being triggered.
Additional context
One of the things going on seems to be that the
ToList()
andToArray()
for some reason cover up the other rules. But if IDE0305's severity is set tonone
orsilent
then nothing is triggered and these lines are "lost". This happens even when the expression type doesn't change between the initialization, the method call or the assignment. I'm guessing that the analyzer just doesn't go "deeper" into the RHS expression since it isn't a direct initialization in these cases and is "wrapped" in a method call?But the first assignment to
list5
is still a direct initialization, I think, even though it isn't at the point thatlist5
is declared. In the case oflist5
I'm guessing the analyzer just doesn't consider the type oflist5
because it isn't being declared there or something?The code I am maintaining has areas that haven't been touched in 10 years and these rules helped me modernize it and simplify a lot of old code but there were a few instances like some of the above that got missed and I found one serendipitously and then had to find the rest (maybe?) with Find and a regular expression. And yes, the original author actually did things like
new List<T>().ToList()
for some reason, and as astatic readonly
that never gets mutated so it probably just should have been anArray
to begin with.In closing, this seems like a combination of a bug in IDE0028/IDE0090 as well as maybe a reason to have either a new "Unnecessary collection copy" rule or a way to configure IDE0305 to only apply to initializations.
The text was updated successfully, but these errors were encountered: