-
Notifications
You must be signed in to change notification settings - Fork 510
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
New rule proposal: use nameof instead of magic strings to refer to parameters #865
Comments
I think this is a great diagnostic, but I would prefer to see it placed in a package of "migration analyzers" instead of this project (see DotNetAnalyzers/Proposals#19). For this project, I would prefer to see the following two rules:
These rules are not limited to C# 6, and validate the two aspects of |
Thanks for reviewing. I think this analyzer is better than just for migration. Folks who have already migrated still need to ensure that old habits or new developers don't regress into using magic strings again.
|
If the migration analyzer I described previously were available and I were working on a C# 6 project, I would likely install it and set that particular diagnostic to severity Error. I see it as very similar to the way this project already uses AsyncUsageAnalyzers to prevent regressions. I could likely be convinced to change the list above to the following:
I definitely don't see this project as the best place for the |
Fair enough. :) |
@sharwell I was just about to request this rule as well. Is it possible to detect the language version in the rule, and just NO-OP? I would vote to add this directly to StyleCop, if at all possible. I don't see this as analogous to |
@sharwell and @AArnott I would love to get a rule that checks if an ArgumentException is using the correct name and also make sure that an ArgumentException always includes a message. This might break some code after an upgrade but I personally think that not using a message and the name of the argument is abuse of the ArgumentException class. I have seen a lot of code where an InvalidOperationException should be thrown instead of an ArgumentException. But maybe there should be an SX version of the rule to make it easy to disable this? Would either of you be willing to create a pull request that describes this rule so we can continue the design of the rule there? I could help out with coding the rule. |
With C# 6 using
nameof(fooparam)
instead of"fooparam"
is preferred because it allows the language service to help keep it up to date in refactorings. A StyleCop rule that finds these magic strings that match parameter names and offers an auto-fix would be awesome.This needs some discussion for how to best detect when a string whose value matches a parameter name should be considered an intended match rather than just a coincidentally same value.
The text was updated successfully, but these errors were encountered: