-
Notifications
You must be signed in to change notification settings - Fork 4k
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
IDE0058 (csharp_style_unused_value_expression_statement_preference) should offer granular (type-specific, method-specific) suppression #47832
Comments
Since the issue I had opened on devcomm was closed as a dupe of this, I would like to offer the more direct alternative of having the analysis rule just not report the methods on StringBuilder (or any well known "Builder" type) as a violation. Having an exclusions list for non-built-in types can be valuable, but we should not have to maintain any built in types in this list and I think that the direct bypassing of StringBuilder by the analysis rule can be turned around much quicker than adding support for an exclusions list. |
I agree with having a useful set of default exclusions of all known in-box
I need it for my own projects anyway.
The problem with any built-in hardcoded list is that the list will go stale very quickly and updating that list probably won't be a priority of the .NET team. The list cannot be automatically-generated either, because .NET assembly metadata does not currently provide a way to indicate which methods Unfortunately using CIL/MSIL bytecode analysis won't help either because the Reference Assemblies don't contain that information at all.
That still leaves out dozens of classes (and hundreds... to thousands?) of methods in the .NET BCL that are just as annoying and people don't like it when tools behave inconsistently or give certain built-in types/features/components special status compared to everybody else. Also, a blanket-list of all I just thought about it more and I realize that even if static-analysis could get a list of all
So for the analyzer to handle this correctly it would need to either:
The best alternative is to allow us to maintain our own exclusion lists. |
Really I just want it to stop reporting StringBuilder, because this CA is very noisy for it, I dont want to have to update the editor.config in 1600 repos every time an exclusion needs to be added. I think our chances of getting this narrower scoped item addressed are better than getting the wider scope. |
I'd recommend following the C++ model here and support a configuration that ONLY triggers this warning/error if the return attribute NoDiscardAttribute (Or equivalent new attrib) isn't present. That way method implementers can clearly state that it's perfectly OK to ignore the return type. I generally find this warning so obnoxious in the "boy that cried wolf" fashion I just disable it outright. |
As an alternative attribute-based proposal (since I dislike C++'s This should then be applied to the fluent-builder methods and several other places such as collection |
As a first step, would it not make sense to update the analyzer to ignore all fluent methods (methods that return the same type as the type on which they were invoked)? Examples:
Are there any reasons to flag fluent methods? As a second step, allow specific fully-qualified methods to be ignored through configuration. This rule is completely useless at the moment, which is unfortunate. See also #34098. |
That wouldn't work for immutable builder methods, as they often do return the same type, Linq's |
Ah, true. It seems that this issue is about different ways of marking return values as ignorable, whereas #34098 is the opposite, marking values as "do not ignore". I wonder what the best approach is. |
I'd say showing the warning unless the return value is marked as can be ignored is way more useful because initially nothing is categorized and you can't expect all libraries that you use to apply do not ignore. Moreover, ignorable return values like those in fluent APIs tend to be grouped together, which makes it easier to mark all of them as can be ignored than the other way around. |
Analyzer
Diagnostic ID: IDE0058
Describe the improvement
I've found the warnings from IDE0058 to be very helpful in catching cases where return values should be used/inspected or where the call-site should explicitly discard the return value, however there are many types built-in to the .NET Framework / .NET Core / etc (and our own projects and third-party libraries) that have methods that return an insignificant value (i.e. it should be explicitly discarded) but these are APIs where adding an explicit discard would add visual noise.
Examples include
StringBuilder.Append(..,)
(which returns itself) and many fluent interfaces.Here's a screenshot of VS2019 complaining about me ignoring the return value from
StringBuilder.Append
. I'm sure you'll agree it's far too squiggly! - but I don't want to blindly suppressIDE0058
(either globally or using#pragma warning disable
inside each relevant class/method/scope) because I want to be informed of other unintentionally ignored return values on other methods.(I note that I'd prefer it if methods didn't return
this
to allow for method-chaining: that's abusing language semantics for the sake of slightly cleaner syntax - C# should have first-class support for syntax like chaining but without requiringreturn this
(and[return: ]
attributes to indicate if a return value actually isthis
or something else) - but I digress...Describe suggestions on how to achieve the rule
I think the safest approach would be to allow us to manually specify in
.editorconfig
every particular method overload where the return-value should be ignored. This may result in a lot of entries being added to.editorconfig
though. To provide for a simpler experience there should be a secondary ability to specify a list of method-owner-types (which can include subclasses with inherited methods) for whichIDE0058
will be automatically ignored or suppressed.Therefore I'm proposing the addition of these new entries to
.editorconfig
for IDE0058:csharp_style_unused_value_assignment_ignore_types
csharp_style_unused_value_assignment_ignore_types_namespaces
csharp_style_unused_value_assignment_ignore_methods
cref
syntax or the .NET fully-qualified type-name (incl generic type backticks and nested-type+
path characters) work just as well.StringBuilder.Append
would ignore all overloads ofAppend
whereasStringBuilder.Append(String)
would only ignore that specific overload.Example
.editorconfig
file:Additional context
Assuming this is possible, the Code Fixes menu should have new Suppression options to suppress either the call-site's specific method overload, all methods with the same name, or all methods on the containing type.
Questions
HashSet<String>.Add
but notHashSet<Int32>.Add
.,
to separate method parameters - but commas are already used to separate values in comma-separated lists in.editorconfig
- so how should those commas be disambiguated?The text was updated successfully, but these errors were encountered: