01: package spoon.vsuite.findbugs.inte;
02:
03: import spoon.processing.AbstractProcessor;
04: import spoon.processing.Property;
05: import spoon.processing.Severity;
06: import spoon.reflect.code.BinaryOperatorKind;
07: import spoon.reflect.code.CtBinaryOperator;
08: import spoon.reflect.code.CtExpression;
09: import spoon.reflect.code.CtLiteral;
10: import spoon.reflect.eval.PartialEvaluator;
11:
12: /**
13: * INT: Integer remainder modulo 1 (INT_BAD_REM_BY_1) Any expression (exp % 1)
14: * is guaranteed to always return zero. Did you mean (exp & 1) or (exp % 2)
15: * instead?
16: *
17: * @author Nicolas Petitprez
18: */
19: public class BadRemByOne extends AbstractProcessor<CtBinaryOperator<?>> {
20:
21: @Property
22: static Severity level = Severity.WARNING;
23:
24: public void process(CtBinaryOperator<?> element) {
25: if (element.getKind() == BinaryOperatorKind.MOD) {
26: PartialEvaluator eval = getFactory().Eval()
27: .createPartialEvaluator();
28: CtExpression<?> e = eval.evaluate(element, element
29: .getRightHandOperand());
30: if (e instanceof CtLiteral
31: && ((CtLiteral<?>) e).getValue() instanceof Number) {
32: if (((Number) ((CtLiteral<?>) e).getValue()).intValue() == 1) {
33:
34: getFactory().getEnvironment().report(this , level,
35: element, "Integer remainder modulo 1");
36: }
37: }
38:
39: }
40:
41: }
42: }
|