Code inspection: Parameter output value is always discarded (non-private accessibility)
Category: Redundancies in Symbol Declarations
ID: OutParameterValueIsAlwaysDiscarded.Global
EditorConfig: resharper_out_parameter_value_is_always_discarded_global_highlighting=[error|warning|suggestion|hint|none]
Language: C#, VB.NET
Requires SWA: Yes
tip
The C# 6.0 "out var" greatly reduces the amount of code needed to use methods with out
parameters — now we almost always declare variables inline and often use type inference to omit variable type: dictionary.TryGetValue(key, out var value).
Another aspect of out
parameters that C# 6.0 has improved is the ability to ignore the output parameter values that we don't need with M(out _)
or M(out var _)
syntax. So it's not uncommon to see invocations like DoSomething(arg, out _, out _)
in modern C# codebase.
JetBrains Rider analyzes such invocations and reports out
parameters that are discarded on all call sites. This helps identify potential problems of a value being always discarded and simplify the signature by removing the unused parameters.
Suboptimal code
class SetOfThings{ private Dictionary<string, int> _set = new(); // Parameter 'value' output value is always discarded public bool HasKey(string key, out int value) { return _set.TryGetValue(key, out value); }}class Test{ public void Usage(SetOfThings set) { if (set.HasKey("abc", out _)) Console.WriteLine("do something"); }}
After the quick-fix
class SetOfThings{ private Dictionary<string, int> _set = new(); public bool HasKey(string key) { int value; return _set.TryGetValue(key, out value); }}class Test{ public void Usage(SetOfThings set) { if (set.HasKey("abc")) Console.WriteLine("do something"); }}
For the solution-wide inspection to work, you need to enable at least one of the following:
Simplified global usage checking: select Show unused non-private type members when solution-wide analysis is off on the Editor | Inspection Settings page of JetBrains Rider settings CtrlAlt0S.
Solution-wide analysis: select Enable solution-wide analysis on the Editor | Inspection Settings page of JetBrains Rider settings CtrlAlt0S.
Note that even if the reported parameter has no direct usages in your solution, there could be cases where it is used indirectly — for example, via reflection — or it could just be designed as public API. In all those cases, you would want to suppress the usage-checking inspection for the parameter in one of the following ways:
The recommended way is to decorate the implicitly used parameter with a code annotation attribute. There are two attributes for this purpose: [UsedImplicitly] and [PublicAPI], which are functionally similar, but let you and your teammates understand how the parameter is actually used.
You can also suppress usage-checking inspections with any custom attribute. To do so, mark the definition of that attribute with the [MeansImplicitUse] attribute.
And finally, you can suppress a specific usage-checking inspection as any other code inspection with a suppression comment or a suppression attribute.