Unused label¶
ID: cs/unused-labelKind: problemSecurity severity: Severity: warningPrecision: highTags: - quality - maintainability - useless-codeQuery suites: - csharp-security-and-quality.qls
Click to see the query in the CodeQL repository
An unused label serves no purpose and clutters the source code. It could be an indication that the code is incomplete, or that the code contains a bug where agoto statement uses the wrong label.
Recommendation¶
Ensure that allgoto statements use the correct label, and remove the label if it is no longer needed.
Another solution is to rewrite the code withoutgoto statements, which can often be much clearer.
Example¶
The following example has an unused labelError, which means that the error message is never displayed. The code can be fixed by either jumping to the correct label, or by rewriting the code withoutgoto statements.
staticvoidMain(string[]args){using(varfile=File.Open("values.xml",FileMode.Create))using(varstream=newStreamWriter(file)){stream.WriteLine("<values>");foreach(vararginargs){uintvalue;if(UInt32.TryParse(arg,outvalue))stream.WriteLine(" <value>{0}</value>",value);elsegotoFinish;// Should be goto Error}gotoFinish;Error:// BAD: Unused labelConsole.WriteLine("Error: All arguments must be non-negative integers");Finish:stream.WriteLine("</values>");}}
References¶
MSDN, C# Reference:goto.