CSharp - Sections of code should not be commented out

Questions:

  • Is there any more documentation around this rule? i.e. an example of complaint vs non complaint code. This seems trivial but when it doesn’t work I start to question my understanding of ‘section’’

Issue:
Commented code not being identified as a code smell. Unfortunately I am not able to share the related code but it is a method with the contents marked with ‘//’ on each line.

Hey there.

Without a reproducer, it won’t be possible to efficiently investigate the issue. I would recommend creating a reproducer that doesn’t include any sensitive code.

Hi Colin,

I added this class to the codebase as an example:

public class TestCodeSmells {

    public static void CommentedOutMethodContentsUsingSingleLine(){
        //DateTime localDate = DateTime.Now;
        //String[] cultureNames = { "en-US", "en-GB", "fr-FR",
        //                        "de-DE", "ru-RU" };

        //foreach (var cultureName in cultureNames) {
        //    var culture = new CultureInfo(cultureName);
        //    Console.WriteLine("{0}: {1}", cultureName,
        //                    localDate.ToString(culture));
        //}
    }

    public static void CommentedOutMethodContentsUsingMulitLine(){
        /*
        DateTime dt = new DateTime(2003, 5, 1);
        Console.WriteLine("Is Thursday the day of the week for {0:d}?: {1}",
                       dt, dt.DayOfWeek == DayOfWeek.Thursday);
        Console.WriteLine("The day of the week for {0:d} is {1}.", dt, dt.DayOfWeek);
        */
    }

    /*
    public static void CommentedOutMethodUsingMultiLine(){
       System.DateTime moment = new System.DateTime(
                                1999, 1, 13, 3, 57, 32, 11);
        // Year gets 1999.
        int year = moment.Year;

        // Month gets 1 (January).
        int month = moment.Month;

        // Day gets 13.
        int day = moment.Day;

        // Hour gets 3.
        int hour = moment.Hour;

        // Minute gets 57.
        int minute = moment.Minute;

        // Second gets 32.
        int second = moment.Second;

        // Millisecond gets 11.
        int millisecond = moment.Millisecond;
    }
    */
}

It still never identified it as a code smell.
Screenshot of rule being active:
ProfileWithRuleEnabled

Would you be able to please try the example code on your side?

Hi Wayne, thanks for reporting the issue and providing a test case.

Indeed the rule documentation is minimal and needs to be more detailed. I’ve added an issue for this: Rule S125: improve rule documentation with examples · Issue #6062 · SonarSource/sonar-dotnet · GitHub

Regarding the code, I’ve checked it and it raised issues in both our unit tests and in the IDE. Do you use any suppressions? If not in SQ/SC maybe in .editorconfig or .globalconfig.

If the rules are not suppressed. Could you please create a small reproducer project for us?

Thank you,
Costin