-
Notifications
You must be signed in to change notification settings - Fork 467
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
Add an analyzer to require optional parameters be specified for internal invocations #1240
Comments
My general preference is to not allow parameters to be omitted when best practice is to include them. For example, I oppose allowing a I would prefer to see one of the following more specific rules instead:
|
Thanks @sharwell. For public library APIs, I think keeping the parameters optional makes for the best user experience. And while I think a rule specific to I really like your suggestion to restrict the rule to where something is available in the current scope. I've implemented this restriction a version of this rule I've added to the project I'm working on. The code can be found here. I've also added a further restriction that for string and primitive-type parameters, the warning should only be shown if the thing that's in scope has the same name (compared case-insensitively). The rule would be too noisy if an optional string was omitted, say, and the rule flags it because there is some other unrelated string in scope. I would be willing to port that rule over to this repo and add an associated VB analyzer. |
@evildour Sorry for the late ping, would you still be willing to port the rule here? |
I think we should write a very simple analyzer |
Also, let us avoid hardcoding any special types such as CancellationToken or such - we want to absolutely avoid opinionated suggestions by hand picking certain types. Instead, we should provide generic API design improvement suggestions in CA rules. |
A code fix (with FixAll) here would be super handy - it would just do a Find all references for the containing method/property symbol, and replace the call sites that use the implicit optional argument with use of an explicit default argument that was used in the original optional parameter definition. class C
{
internal void M(object o = null) // changed to 'M(object o)' by the code fix
{
}
internal void M1()
{
M(); // changed to 'M(o: null);' by the code fix
M(new object()); // not changed by code fix
}
} |
Analyzer package
Microsoft.CodeQuality.Analyzers
Analyzer
InternalOptionalParametersRequired
Repro steps
Consider the following code:
Obviously, the first overload of
DoSomething
should have passedoptions
to the 2nd overload when it called it. To catch this kind of issue, there should be an analyzer available which will warn when invocations to other members defined within the same assembly omit optional parameters.Note: I would be willing to implement this.
The text was updated successfully, but these errors were encountered: