Skip to content

Commit

Permalink
Add compare_nan fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscerie committed Oct 14, 2023
1 parent 63ad3eb commit 1c519dc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
14 changes: 8 additions & 6 deletions selene-lib/src/lints/compare_nan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ impl Lint for CompareNanLint {
.comparisons
.iter()
.map(|comparisons| {
let fixed_code = format!(
"{variable} {operator} {variable}",
variable = comparisons.variable,
operator = comparisons.operator,
);

Diagnostic::new_complete(
"compare_nan",
"comparing things to nan directly is not allowed".to_owned(),
Label::new(comparisons.range),
vec![format!(
"try: `{variable} {operator} {variable}` instead",
variable = comparisons.variable,
operator = comparisons.operator,
)],
vec![format!("try: `{}` instead", fixed_code.clone())],
Vec::new(),
None,
Some(fixed_code),
)
})
.collect()
Expand Down
34 changes: 34 additions & 0 deletions selene-lib/tests/lints/compare_nan/compare_nan_if.fixed.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-if x == 0/0 then
+if x ~= x then
end

-if x ~= 0/0 then
+if x == x then
end

if x ~= x then
end

if x >= 0/0 then
end

if 1 == 0/0 then
end

if 1 ~= 0/0 then
end

if 1 + 0/0 then
end

if y.foo() == 0/0 then
end

if y.bar() ~= 0/0 then
end

if x == 1 then
end

if 1 == 0 then
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local _ = 0/0
local _ = 1 ~= 0/0
local _ = 2 == 0/0
local _ = 3 + 0/0
local _ = 4 >= 0/0
-local _ = x ~= 0/0
-local _ = x == 0/0
+local _ = x == x
+local _ = x ~= x
local _ = x >= 0/0
local _ = y.foo() == 0/0
local _ = y.bar() ~= 0/0

local _ = x == 1
local _ = 1 == 0

0 comments on commit 1c519dc

Please sign in to comment.