-
Notifications
You must be signed in to change notification settings - Fork 470
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
CA2241 - Possibility to support custom string formatting methods? #2799
Comments
Yes, certainly. I am planning to address bunch of the configurable functionality requests today, and this seems a reasonable request. |
…for CA2241 [CA2241](https://docs.microsoft.com/visualstudio/code-quality/ca2241-provide-correct-arguments-to-formatting-methods) considers String.Format and Console.WriteLine as special formatting methods to validate the callsites for formatting arguments. This change allows end users to supply additional formatting methods that should be validated. Fixes dotnet#2799
This would be better served with an attribute introduced by the BCL that would mark format template and parameters. |
Trying to get standard attributes into the BCL is hard (we are still waiting on IDisposable ones) The pattern using the parameters: 'string format, params object[] arguments' is so common, should it not detect this instead of forcing everybody to even add standard methods like StringBuilder.AppendFormat to every .editorconfig file? In our codebase we would have to add dozens of methods I might give adding this a try myself. |
What do you mean by that? |
StringBuilder.AppendFormat is a 'string formatting' method that is not hard-coded in the source code of the analyzer. By default therefore the rule will not check parameters.
The analyzer only complains about the last line missing a parameter. |
@manfred-brands Does your editorconfig lie in the project folder or repo folder? If it’s the latter, you might need to move to a 3.3.x prerelease package. Can you check https://github.com/dotnet/roslyn-analyzers/blob/master/docs/Analyzer%20Configuration.md#enabling-editorconfig-based-configuration? |
I added a pull request for this: #3653. |
@mavasani The .editorconfig is above the projects as it is common to all projects in the solution. The config works for enabling/disabling rules, so it is read. I will try the workaround. |
@mavasani For the workaround to work, it looks I need to have two copies of .editorconfig. One in the solution directory that is used to find what rules are configured and the other one in the specified location to find the additional configuration for the rules. |
Analyzer package
Microsoft.NetCore.Analyzers
Package Version
v2.9.4
Diagnostic ID
CA2241
Repro steps
Have a custom method that accepts the typical format string + params args.
IE:
public static string Log(string format, params object[] args)
{
...
}
Expected behavior
Log("{1}", 2) triggers CA2241
Actual behavior
CA2241 is hardcoded to look at Console.Write(Line) and string.Format, so no error.
I assume adding this would be a breaking change WRT backwards compatibility, but could this be made configurable?
The text was updated successfully, but these errors were encountered: