Repository navigation
SONARJAVA-7215 Do not report S2695 when part of the query cannot be resolved - #6379
Conversation
…esolved The placeholder count was a lower bound compared as if it were an upper bound: an unresolvable query fragment was counted as zero placeholders, so the rule asserted "has no parameters" on queries it had not resolved. - propagate unknown through concatenation and "+=" instead of substituting zero for an unresolved fragment - resolve a conditional expression as the maximum of its branches - count over the folded constant via asConstant, which resolves cross-class constants, whole constant chains and text blocks Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
150fbea to
e3724dd
Compare
…ment Propagating "unknown" through every unresolved operand went too far: a non-constant number or boolean concatenated into the query made the whole count unknown, dropping correct reports on queries such as "... WHERE id = ? LIMIT " + limit. Such an operand cannot contribute a placeholder whatever its runtime value, so it now counts as zero and the surrounding count stays exact. Characters are excluded, since '?' is a valid one. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
/pvf |
|
📊 Performance Validation complete |
lijun-chen-sonarsource
left a comment
There was a problem hiding this comment.
Overall good improvements!
I left some comments and questions.
A char concatenated into the query was treated as wholly unknown, which made the surrounding count unknown too. But a char is one character, so it adds at most one placeholder: the count is bounded, not unresolvable, and an index past that bound is still reportable. Replace the boolean guard with a bound - zero for a number or boolean, one for a character, unknown for anything else. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…ONARJAVA-7228 Constant folding renders a char as its numeric value, so a query built as "WHERE id = " + QUESTION_MARK resolves to "WHERE id = 63" and the rule reports a valid index 1 as out of range. The defect is in ExpressionUtils, not in the check: it corrupts every asConstant(String.class) caller that folds a concatenation holding a char constant. Record the false positive here and fix it in SONARJAVA-7228. Also drop conditionalClauseGuardedBySameCondition: its ternary path is already covered by ternaryWithDifferentBranches and its constant path by constantsFromAnotherClass, and the rule does not inspect the enclosing condition its name refers to. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
3d74a5a to
1f8701a
Compare
Code Review ✅ Approved 4 closed / 4 findings🟡 Medium risk · Updating S2695 placeholder counting for unresolved fragments and conditional branches could suppress valid excess-parameter findings if the new bounds are wrong. Fixes S2695 false positives by propagating unknown placeholder counts through concatenation, ✅ 4 closed✅ Edge Case: Numeric operands now make the whole query unknown, hiding real issues
✅ Edge Case: Known char literals are counted as a possible '?' placeholder
Closed: Quality: charLocalVariableFragment comment misstates what the test exercises
Closed: Edge Case: Escaped '?' char literals are counted as zero placeholders
Verification Rules 2/2 metFunctional validation No results Rules 2 passed 1 not applicable
Auto-approval Not enabledFor more verification details, comment OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|




The placeholder count was a lower bound compared as if it were an upper bound: an unresolvable query fragment was counted as zero placeholders, so the rule asserted "has no parameters" on queries it had not resolved.
Summary by Gitar
'?'is a valid placeholdercharconstant'?'concatenated into a query is a false positive, tracked bySONARJAVA-7228This will update automatically on new commits.