Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Expression always evaluates to the same value

ID: java/evaluation-to-constantKind: problemSecurity severity: Severity: warningPrecision: very-highTags:   - quality   - reliability   - correctnessQuery suites:   - java-security-and-quality.qls

Click to see the query in the CodeQL repository

Some expressions always evaluate to the same result, no matter what their subexpressions are:

  • x*0 always evaluates to0.

  • x%1 always evaluates to0.

  • x&0 always evaluates to0.

  • x||true always evaluates totrue.

  • x&&false always evaluates tofalse.Wheneverx is not constant, such an expression is often a mistake.

Recommendation

If the expression is supposed to evaluate to the same result every time it is executed, consider replacing the entire expression with its result.

Example

The following method tries to determine whetherx is even by checking whetherx%1==0.

publicbooleanisEven(intx){returnx%1==0;//Does not work}

However,x%1==0 is always true whenx is an integer. The correct check isx%2==0.

publicbooleanisEven(intx){returnx%2==0;//Does work}

References


[8]ページ先頭

©2009-2025 Movatter.jp