Kampfkarren / selene

A blazing-fast modern Lua linter written in Rust
https://kampfkarren.github.io/selene/
Mozilla Public License 2.0
591 stars 75 forks source link

Port duplicate condition lint from Luau #273

Open Kampfkarren opened 3 years ago

Kampfkarren commented 3 years ago

DuplicateCondition (24)

When checking multiple conditions via and/or or if/elseif, a copy & paste error may result in checking the same condition redundantly. This almost always indicates a bug, so a warning is emitted when use of a duplicate condition is detected.

assert(self._adorns[normID1] and self._adorns[normID1]) -- Condition has already been checked on column 8
matthargett commented 3 years ago

some additional test cases:

while foo and foo or bar do
end
while foo or foo and bar do
end
repeat 
--[[...]]
until not foo and not foo
repeat 
--[[...]]
until foo or not foo

since derefs can mean index or newindex comes into play, it may not safe to assume the value of the deref is immutable and therefore the presence of duplicated tokens doesn't mean double-checking for the same value

repeat
--[[...]]
until states.fieldWithMaybeSideEffect or states.fieldWithMaybeSideEffect
Kampfkarren commented 3 years ago

We already assume __index doesn't have side-effects in a few places, I think, such as duplicate_condition.