JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C#


CSHARP.STRUCT.RC : Redundant Condition (C#)

Summary

A test is always true or always false and can thus be removed.

This checker looks for tests that are useless since, for instance, their outcome drives control to the same execution path, regardless of the test itself. This happens for instance when the empty statement is used after a conditional because the programmer put a semicolon after the conditional. This checker also looks for tests that always have the same outcome (always true or always false).

Properties

Class Name Redundant Condition (C#)
Significance reliability
Mnemonic CSHARP.STRUCT.RC
Categories
CWE CWE:570 Expression is Always False
  CWE:571 Expression is Always True
Availability Available for C# only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Redundant Condition (C#)"

Example

using System;

namespace DocumentationExamples
{

    public class UselessTest
    {
        public static void Main(string[] args)
        {
            if (args.Length < 2) ;  // Empty Branch Statement (C#) warning issued here 
            Environment.Exit(-1);
            Go(DateTime.Now.Millisecond % 4 == 0, DateTime.Now.Millisecond % 3 == 0);
            Test(17);
            object o = new object();
            if (o != new object())  // Redundant Condition (C#) warning issued here 
                Console.WriteLine("distinct");
        }
        public static void Go(bool b1, bool b2)
        {
            if (b1 = true)          // Redundant Condition (C#) warning issued here 
                Console.WriteLine("yes");
        }
        private static void Test(int i)
        {
            if (i == 17)            // Redundant Condition (C#) warning issued here 
                Console.WriteLine("17");
        }
    }
}

Resolution

Remove the check or complete a conditional with actual then or else branch, instead of an empty statement. Verify if the useless test is the sign of a more significant algorithmic problem.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.