File tree 2 files changed +17
-8
lines changed
lib/codeql/rust/elements/internal
test/query-tests/unusedentities
2 files changed +17
-8
lines changed Original file line number Diff line number Diff line change @@ -387,27 +387,36 @@ module Impl {
387
387
override string getAPrimaryQlClass ( ) { result = "VariableAccess" }
388
388
}
389
389
390
- /** Holds if `e` occurs in the LHS of a ( compound) assignment. */
391
- private predicate assignLhs ( Expr e ) {
392
- exists ( BinaryExpr be |
393
- be .getOperatorName ( ) .regexpMatch ( ".*=" ) and
390
+ /** Holds if `e` occurs in the LHS of an assignment or compound assignment. */
391
+ private predicate assignLhs ( Expr e , boolean compound ) {
392
+ exists ( BinaryExpr be , string op |
393
+ op = be .getOperatorName ( ) .regexpCapture ( "(.*)=" , 1 ) and
394
394
e = be .getLhs ( )
395
+ |
396
+ op = "" and compound = false
397
+ or
398
+ op != "" and compound = true
395
399
)
396
400
or
397
401
exists ( Expr mid |
398
- assignLhs ( mid ) and
402
+ assignLhs ( mid , compound ) and
399
403
getImmediateParent ( e ) = mid
400
404
)
401
405
}
402
406
403
407
/** A variable write. */
404
408
class VariableWriteAccess extends VariableAccess {
405
- VariableWriteAccess ( ) { assignLhs ( this ) }
409
+ VariableWriteAccess ( ) { assignLhs ( this , _ ) }
406
410
}
407
411
408
412
/** A variable read. */
409
413
class VariableReadAccess extends VariableAccess {
410
- VariableReadAccess ( ) { not this instanceof VariableWriteAccess }
414
+ VariableReadAccess ( ) {
415
+ not this instanceof VariableWriteAccess
416
+ or
417
+ // consider LHS in compound assignments both reads and writes
418
+ assignLhs ( this , true )
419
+ }
411
420
}
412
421
413
422
cached
Original file line number Diff line number Diff line change @@ -109,7 +109,7 @@ fn arrays() {
109
109
110
110
println ! ( "lets use {:?}" , js) ;
111
111
112
- for k // BAD : unused variable [SPURIOUS: macros not yet supported]
112
+ for k // SPURIOUS : unused variable [macros not yet supported]
113
113
in ks
114
114
{
115
115
println ! ( "lets use {}" , k) ;
You can’t perform that action at this time.
0 commit comments