FSharpLint icon indicating copy to clipboard operation
FSharpLint copied to clipboard

New rule proposal: Avoid commented-out lines of code

Open milbrandt opened this issue 4 years ago • 2 comments

Description

Commented-out lines should be removed before release. It might already be broken. If old code is later required again, it will be available in source code management system.

See also:

Expected behavior

Report any commented-out code.

  • Report multi-line commented code
let animal = "bird"

// Test the length of the string.
if (animal.Length = 1) then
    // Not reached.
    printfn "A"
(*
elif (animal.Length = 2) then
    // Not reached.
    printfn "B"
*)
else
    // This statement is reached.
    printfn "C"
  • Report single line commented code
assignment.
let result =
    if count >= 200 then 1
    // elif count <= 100 then 2
    else 3

Actual behavior

Rule not available today.

Known workarounds

One might configure a hint

Related information

Rule not listed at http://fsprojects.github.io/FSharpLint/Rules.html

milbrandt avatar Mar 23 '20 19:03 milbrandt

I think this would be a useful rule to have, but it may be difficult to implement. How can we tell apart normal comments from commented-out code? We would need to come up with some heuristic.

jgardella avatar May 18 '20 21:05 jgardella

I have not investigated how other analysers do the job. But there are some, which do that for other languages, like the C# analyzer, Rule S125 from SonarQube.

Without knowing in detail what they do my first idea:

  • XML doc comments are always valid and will not be reported
  • other commented lines "remove the comment and try non-empty lines to tokenize".
  • have a look at src/SonarAnalyzer.CSharp/Rules/CommentedOutCode.cs in the above mentioned C# analyzer

milbrandt avatar May 19 '20 06:05 milbrandt