Skip to content

Commit

Permalink
Prevent false positive by relaxing variable use (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe committed Jan 14, 2021
1 parent 2e870fd commit 5d4ec26
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/plugins/trackCodeFlow/varTracking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BscFile, FunctionExpression, BsDiagnostic, Range, isForStatement, isForEachStatement, isIfStatement, isAssignmentStatement, Expression, isVariableExpression, isBinaryExpression, TokenKind, Scope, CallableContainerMap, DiagnosticSeverity, isLiteralInvalid } from 'brighterscript';
import { BscFile, FunctionExpression, BsDiagnostic, Range, isForStatement, isForEachStatement, isIfStatement, isAssignmentStatement, Expression, isVariableExpression, isBinaryExpression, TokenKind, Scope, CallableContainerMap, DiagnosticSeverity, isLiteralInvalid, isWhileStatement } from 'brighterscript';
import { LintState, StatementInfo, NarrowingInfo, VarInfo } from '.';
import { BsLintRules } from '../..';

Expand Down Expand Up @@ -209,6 +209,7 @@ export function createVarLinter(
parent.locals = locals;
} else {
const isParentIf = isIfStatement(parent.stat);
const isLoop = isForStatement(closed.stat) || isForEachStatement(closed.stat) || isWhileStatement(closed.stat);
locals.forEach((local, name) => {
const parentLocal = parent.locals.get(name);
// if var is an iterator var, flag as partial
Expand All @@ -227,6 +228,13 @@ export function createVarLinter(
if (parentLocal?.isIterator) {
local.isIterator = parentLocal.isIterator;
}
if (!local.isUsed && isLoop) {
// avoid false positive if a local set in a loop isn't used
const someParentLocal = findLocal(local.name);
if (someParentLocal?.isUsed) {
local.isUsed = true;
}
}
parent.locals.set(name, local);
});
}
Expand Down
19 changes: 19 additions & 0 deletions test/project1/source/unused-variable.brs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@ sub ok3()
print a
end if
end sub

sub ok4()
a = false
b = false
list = ["A", "B"]
for i = 0 to list.count() - 1
if list[i] = "A"
a = true
if b
exit for
end if
else if list[i] = "B"
b = true 'assume used because `b` could have been used in another branch of the loop
if a
exit for
end if
end if
end for
end sub

0 comments on commit 5d4ec26

Please sign in to comment.