IntelliJ-Luanalysis icon indicating copy to clipboard operation
IntelliJ-Luanalysis copied to clipboard

Is it doable to add a UnlogicalComparisonInspector?

Open pouwelsjochem opened this issue 3 years ago • 2 comments

Environment

name version
IDEA version IC2020.3
Luanalysis version 1.2.2
OS MacOS Big Sur

What are the steps to reproduce this issue?

Not really an issue, but more of an idea to discuss.

What happens?

When using the following code, no warning or error is given:

---@alias Direction "left"|"right"|"up"|"down"

local myDirection = math.random() > 0.5 and "right" or "left" ---@type Direction
if myDirection == "ight" then

end

What were you expecting to happen?

An Intellij inspection to turn on these types of errors, effectively creating enumerations with zero performance cost.

pouwelsjochem avatar Mar 09 '21 09:03 pouwelsjochem

Something like a "Condition is always false" inspection seems reasonable and can be inferred from the types involved.

However, I'd probably advise against using string literals for this purpose The performance win will be small but the maintenance burden will be significant. Say for example you wished to change the contents of a string literal type in an aliased union. You can't easily refactor string literals and search and replace will give you false positives.

Benjamin-Dobell avatar Mar 09 '21 12:03 Benjamin-Dobell

A "Condition is always false" inspection sounds good! Maybe even a "Condition is always true" inspection to discourage making unnecessary conditions.

The performance win is definitely neglectable, but string literals are actually often returned in the engine we use (Solar2D). For example a KeyEvent having a phase which can only be "up"|"down". That's the reason why I used string literals in this example :)

pouwelsjochem avatar Mar 09 '21 12:03 pouwelsjochem