summaryrefslogtreecommitdiff
path: root/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java
blob: 9a10262e31481a75bcf0fccef70aac7a025376e7 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.siyeh.igtest.numeric.pointless_arithmetic_expression;

import java.util.Random;

public class PointlessArithmeticExpression
{
    private static final int ZERO_CONSTANT = 0;
    private static final int ONE_CONSTANT = 1;

    public static void main(String[] args)
    {
        final int i = 2;
        final int j = <warning descr="'i + 0' can be replaced with 'i'">i + 0</warning>;
        System.out.println(j);
        int k = <warning descr="'0+j' can be replaced with 'j'">0+j</warning>;
        System.out.println(k);
         k = <warning descr="'j - 0' can be replaced with 'j'">j - 0</warning>;
        System.out.println(k);
        k = 0 - j;
        System.out.println(k);
        k = j * ZERO_CONSTANT;
        System.out.println(k);
        k = j * ONE_CONSTANT;
        System.out.println(k);
        k = <warning descr="'j / 1' can be replaced with 'j'">j / 1</warning>;
        System.out.println(k);
        String string = "foo" + 0;

        k = <warning descr="'j%1' can be replaced with '0'">j%1</warning>;
        System.out.println(k);
               
        if(k<=Integer.MAX_VALUE)
        {

        }
        if(k>=Integer.MIN_VALUE)
        {

        }
        if(k>Integer.MAX_VALUE)
        {

        }
        if(k<Integer.MIN_VALUE)
        {

        }
        if(Integer.MAX_VALUE >= k)
        {

        }
        if(Integer.MIN_VALUE <= k)
        {

        }
        if(Integer.MAX_VALUE < k)
        {

        }
        if(Integer.MIN_VALUE > k)
        {

        }
    }

    double boom(double d){
        return 1.1 * d;
    }

    void doubleDoom(int i) {

        if (i > Integer.MAX_VALUE) {
            System.out.println("always false!");
        }
        if (i < Integer.MAX_VALUE) {
            System.out.println("do nothing");
        }
        if (i <= Integer.MAX_VALUE) {
            System.out.println("always true");
        }

        if (i >= Integer.MIN_VALUE) {
            System.out.println("sometimes possible");
        }
        if (i < Integer.MIN_VALUE) {
            System.out.println("always false");
        }
    }

    double floatsOrDoubles() {
        return 123.001 % 1.0;
    }

    void more(int i) {
        System.out.println(<warning descr="'i / i' can be replaced with '1'">i / i</warning>);
        System.out.println(<warning descr="'i - i' can be replaced with '0'">i - i</warning>);
        System.out.println(<warning descr="'i % i' can be replaced with '0'">i % i</warning>);
    }
}
class Main {
  private static final int CONST = 9;
  int i;
  Main(int i) {
    this.i = i;
  }

  static int doo() {
    return new Main(1).i - new Main(0).i;
  }

  int fly(int i) {
    final Main main = new Main(12);
    return (CONST + (new Main(5).i) * 8) - (Main.CONST + new Main(5).i * (8));
  }

  int one = <warning descr="'5/5' can be replaced with '1'">5/5</warning>;
}
class Expanded {{
  int m = <warning descr="'1/**/ - (byte)0 - 9' can be replaced with '1/**/ -  9'">1/**/ - (byte)0 - 9</warning>; // warn
  int j = <warning descr="'8 * 0 * 8' can be replaced with '0'">8 * 0 * 8</warning>;
  int k = <warning descr="'1 + /*a*/0 +/**/ 9' can be replaced with '1 + /*a*//**/ 9'">1 + /*a*/0 +/**/ 9</warning>;
  byte l = (byte) (<warning descr="'1L - 1L' can be replaced with '0L'">1L - 1L</warning>);
  byte u = 1;
  int z = <warning descr="'2 / 1 / 1' can be replaced with '2 /  1'">2 / 1 / 1</warning>;
  System.out.println(<warning descr="'u * 1' can be replaced with 'u'">u * 1</warning>);
  long g = <warning descr="'8L / 8L' can be replaced with '1L'">8L / 8L</warning>;
  long h = <warning descr="'9L * 0L' can be replaced with '0L'">9L * 0L</warning>;
  int a = 8 * 0 * 8 *<error descr="Expression expected"> </error>; // don't warn
  int minus = 2 - 1 - 1;
  int div = 3 / 2 / 2;
  int mod = 3 % 2 % 2;
}}
class SideEffects {
  public static void main( String args[] ){
    Random rand = new Random();
    int array[] = {1, 2, 4};
    int i = 1;
    int b = array[i++] - array[i++];
    System.out.println(rand.nextInt(1000) - rand.nextInt(1000));
  }
}