aboutsummaryrefslogtreecommitdiff
path: root/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariableMultipleAndNestedConditions.java
blob: 40706ddf9e876d0c0aa212a98120963e15d92fc6 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.puppycrawl.tools.checkstyle.checks.coding;

public class InputFinalLocalVariableMultipleAndNestedConditions {

    // False positive
    // https://github.com/checkstyle/checkstyle/issues/3186
    void method() {
        for (int i = 0; i < 2; i++) {
            final Object converter = new Object();
            final String type = getType();
            Object value;

            if ("s1".equals(type)) {
                if (getCondition(1)) {
                    value = getValue(1);
                }
                else {
                    continue;
                }
            }
            else if ("s2".equals(type)) {
                if (getCondition(2)) {
                    value = getValue(2);
                }
                else {
                    continue;
                }
            }
            else {
                continue;
            }

            if (converter != null) {
                value = /* converter. */getValue(1, type, value);
            }
        }
    }

    // False positive
    // https://github.com/checkstyle/checkstyle/issues/3186
    void method2() {
        for (int i = 0; i < 2; i++) {
            final Object converter = new Object();
            final Object element = new Object();
            String name;

            if (getCondition(1)) {
                name = "1";
            } else if (getCondition(2)) {
                name = "2";
            } else {
                continue;
            }

            if (converter != null) {
                name = /* converter. */getName(element, name);

                if (name == null)
                    continue;
            }
        }
    }

    public Object getValue(int i) {
        return null;
    }
    public Object getValue(int i, String type, Object value) {
        return value;
    }
    public boolean getCondition(int i) {
        return true;
    }
    public String getType() {
        return "s1";
    }
    private String getName(Object element, String name) {
        return "s";
    }
}