aboutsummaryrefslogtreecommitdiff
path: root/src/test/resources/com/puppycrawl/tools/checkstyle/checks/InputStringLiteralEquality.java
blob: d6609d39af0452461800a5b3010e1775cccbf2f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.puppycrawl.tools.checkstyle.checks;

public class InputStringLiteralEquality
{
    void foo(String name)
    {
        if (name == "Lars")
        {
            // flagged, should use equals
        }

        if ("Oleg" == name)
        {
            // flagged, should use equals
        }

        if ("Oliver" == "Oliver")
        {
            // doesn't make much sense because this can be evaluated
            // to true at compile time, but is flagged anyway
        }

        String compare = "Rick";
        if (name == compare)
        {
            // currently not flagged.
            //
            // Implementing this is very complicated, we would need
            // - type info on the == operands
            // - prevent false alarms where the user explicitly wants
            //   to compare object identities
            //
            // My current feeling is that we should leave finding
            // this one to manual code inspections. After all MCI is
            // what some of us get paid for :-)
        }

        if ("Rick".toUpperCase() == "Rick".toLowerCase())
        {
            // completly dynamic, don't flag
        }
    }
}