github / github/codeql

[Rust] Unused variable false positive in compound conditional statements

Open
#20,201 3 comments 0 reactions 0 assignees View on GitHub
false-positive
Dominant language
CodeQL
Stars
10.1k
Forks
2.1k
Avg merge
2d 15h
Merged PRs (30d)
141

Description

**Description of the false positive**

When there is an if-let statement, and multiple conditions exist in the conditional check (either AND or OR as operators), CodeQL flags the original variable as unused if it is used in subsequent conditional statements.

**Code samples or links to source code**

There are many dismissed false positives linked to this merged PR:

A small example would be:

```rust
for result in builder.build() {
match result {
Ok(entry) => {
if let Some(file_type) = entry.file_type()
&& file_type.is_file()
{
paths.push(entry.path().to_path_buf());
}
}
Err(msg) => eprintln!("Error reading entry: {msg}"),
}
}
```

Here, `file_type` was flagged as a false positive, even though it is used in the following boolean statement `file_type.is_file()`.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the Rust example and compare it with the dismissed false positives linked from seiri PR 57. Trace the CodeQL unused-variable analysis for if-let conditions with compound boolean operators; no source file or test is named in the issue. Done means the variable is no longer reported when used in a subsequent condition, with coverage for the shown case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.