Rider Help

Code Inspection: Assignment is not used

By analyzing the control flow of your code Rider is able to detect redundant initializers of fields and local variables. If the value you assign is not used until the next assignment is made in any of the execution paths, Rider suggests to remove the redundant initializer.

This inspection also works when you assign a new value to a parameter that was never used beforehand. In this case, Rider doesn't suggest any fixes because it is not clear whether the parameter should be replaced with a local variable or whether the assignment is erroneous:

void Bar(string param) { param = "something new"; Console.WriteLine(param); }

Redundant assignments can occur in different situations. For example, when a variable is initialized with the default value, or, like shown below, when some non-default value is assigned, but never gets used. Here you can see that myDoc is initialized with a new instance of XDocument, but the next line assigns it to either a or b anyway, so the initially created object is nothing but a new task for the garbage collector.

Here is an example of a quick-fix suggested by this inspection:

Suboptimal codeAfter the quick-fix
XElement GetRoot(bool flag, XDocument a, XDocument b) { var myDoc = new XDocument(); myDoc = flag ? a : b; return myDoc.Root; }
XElement GetRoot(bool flag, XDocument a, XDocument b) { XDocument myDoc; myDoc = flag ? a : b; return myDoc.Root; }
Last modified: 11 October 2017

See Also