JetBrains Rider 2021.2 Help

Code Inspection: Assignment is not used

By analyzing the control flow of your code JetBrains 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, JetBrains Rider suggests removing the redundant initializer.

This inspection also works when you assign a new value to a parameter that was never used beforehand. In this case, JetBrains 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:

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: 08 March 2021