gccrs: Fix unused_assignment check logic
Improves gccrs's unused assignment linting by tracking only the last assignment.
This patch refines gccrs’s unused_assignment linting logic to correctly identify unused variables. It ensures that only the most recent assignment to a variable is tracked. If a variable is assigned a new value before the previous assignment has been used, the older assignment is now correctly flagged as potentially unused, preventing false positives. This addresses a bug where previous assignments might be overlooked.
In Details
The UnusedChecker::visit method in rust-unused-checker.cc has been updated to manage assignments more precisely. UnusedContext::add_assign now uses a new set to track assignments, and remove_assign and is_variable_assigned are adjusted to consider only the last assignment per variable. A new helper function, is_assign_unused, is introduced. This change ensures that the linter accurately identifies variables that are assigned a value but never read, particularly in cases of multiple assignments to the same variable.
- unused assignment
- A situation in programming where a variable is assigned a value, but that value is never subsequently read or used before being overwritten or going out of scope. Compilers can often warn about this to help prevent bugs and dead code.
- linting
- The process of analyzing source code to flag programming errors, stylistic errors, and suspicious constructs. Linters help improve code quality and prevent bugs.
- variable
- A named storage location that can hold a value, which may change during program execution.
- assignment
- The process of storing a value in a variable.
- UnusedChecker
- A component within the gccrs compiler responsible for detecting unused variables, assignments, and other code constructs.