C#


CSHARP.COMPARE.EMPTYSTR : Comparison to Empty String (C#)

要旨

A string is compared to the empty string instead of using IsEmpty().

This checker identifies incorrect or inefficient comparisons through Equals() or ==. In some cases, these comparisons are wrong: for instance, strings should be compared for equality through Equals() rather than through ==. Classes can be safely compared through == instead. In other cases, these comparisons can be replaced by more efficient code.

プロパティ

クラス名 Comparison to Empty String (C#)
日本語クラス名 Comparison to Empty String (C#)
クラス分類 信頼性 (reliability)
ニーモニック CSHARP.COMPARE.EMPTYSTR
カテゴリー
CWE CWE:597 Use of Wrong Operator in String Comparison
対応言語 C# で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Comparison to Empty String (C#)"

using System;
using System.Diagnostics;

namespace DocumentationExamples
{

    public class BadEq
    {
        public static bool verbose;
        private readonly static string[] arr = new string[] { "verbose" };
        public static void Main(string[] args)
        {
            if (args.Length > 0)
                Debug.Assert(!args[0].Equals(""));   // Comparison to Empty String (C#) warning issued here
            if (args.Length > 0 && args[0] == "verbose")
                verbose = true;
            if (args.Equals(arr))                    // equals on Array (C#) warning issued here
                verbose = true;
            if (args.Length > 0)
            {
                BadEq m = new BadEq();
                Console.WriteLine(m.Test(args[0]));
                Console.WriteLine(m.WeAreRedefined());
            }
        }
        private bool Test(object o)
        {
            return o == this;                        // == Always Fails (C#)  warning issued here
                                                     // - operands have incompatible types
        }
        public bool WeAreRedefined()
        {
            return !GetType().Equals(typeof(BadEq)); // Missing Equals Override (C#) warning issued here
        }
        
        public bool EqualityTest()
        {
            object name = "myname";
            char[] values = { 'm', 'y', 'n', 'a', 'm', 'e' };
            object myName = new string(values);
            return name == myName;                   // Should Use equals() Instead of == (C#) warning issued here
        }
    }
}

In this example, the programmer should rewrite the code as follows:

public static bool verbose;
private readonly static string[] arr = new string[] { "verbose" };
public static void Main(string[] args)
{
    if (args.Length > 0)
        Debug.Assert(!string.IsNullOrEmpty(args[0]));
    if (args.Length > 0 && args[0].Equals("verbose"))
        verbose = true;
    if (args.SequenceEqual(arr))
        verbose = true;
    if (args.Length > 0)
    {
        BadEq m = new BadEq();
        //Console.WriteLine(m.Test(args[0]));
        Console.WriteLine(m.WeAreRedefined());
    }
}

public bool WeAreRedefined()
{
    return GetType() != typeof(BadEq);
}

解決法

Check if the equality is actually wrong or can be replaced by more optimized code.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。