Skip to content

Commit 4aec44f

Browse files
giacomocavalierilpil
authored andcommitted
highlight double negations as safe to remove
1 parent b9ed09d commit 4aec44f

7 files changed

+46
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,8 @@
107107
- Fixed a bug where `echo` could crash on JavaScript if the module contains
108108
record variants with the same name as some built-in JavaScript objects.
109109
([Louis Pilfold](https://github.com/lpil))
110+
111+
- Fixed a bug where the compiler would highlight an entire double negation
112+
expression as safe to remove, instead of just highlighting the double
113+
negation.
114+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

compiler-core/src/type_/expression.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -884,9 +884,18 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
884884
.error(convert_unify_error(error, value.location()))
885885
}
886886

887-
if let TypedExpr::NegateBool { .. } = value {
887+
if let TypedExpr::NegateBool {
888+
location: inner_location,
889+
..
890+
} = value
891+
{
888892
self.problems
889-
.warning(Warning::UnnecessaryDoubleBoolNegation { location });
893+
.warning(Warning::UnnecessaryDoubleBoolNegation {
894+
location: SrcSpan {
895+
start: location.start,
896+
end: inner_location.start + 1,
897+
},
898+
});
890899
}
891900

892901
TypedExpr::NegateBool {
@@ -903,16 +912,34 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
903912
.error(convert_unify_error(error, value.location()));
904913
}
905914

906-
if let TypedExpr::Int { value: ref v, .. } = value
915+
if let TypedExpr::Int {
916+
value: ref v,
917+
location: ref inner_location,
918+
..
919+
} = value
907920
&& v.starts_with('-')
908921
{
909922
self.problems
910-
.warning(Warning::UnnecessaryDoubleIntNegation { location });
923+
.warning(Warning::UnnecessaryDoubleIntNegation {
924+
location: SrcSpan {
925+
start: location.start,
926+
end: inner_location.start + 1,
927+
},
928+
});
911929
}
912930

913-
if let TypedExpr::NegateInt { .. } = value {
931+
if let TypedExpr::NegateInt {
932+
location: inner_location,
933+
..
934+
} = value
935+
{
914936
self.problems
915-
.warning(Warning::UnnecessaryDoubleIntNegation { location });
937+
.warning(Warning::UnnecessaryDoubleIntNegation {
938+
location: SrcSpan {
939+
start: location.start,
940+
end: inner_location.start + 1,
941+
},
942+
});
916943
}
917944

918945
TypedExpr::NegateInt {

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__warnings__double_unary_bool_literal.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,4 @@ warning: Unnecessary double negation (!!) on bool
1010
┌─ /src/warning/wrn.gleam:1:25
1111
1212
1pub fn main() { let _ = !!True }
13-
^^^^^^
14-
15-
Hint: You can safely remove this.
13+
^^ You can safely remove this.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__warnings__double_unary_bool_variable.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,4 @@ warning: Unnecessary double negation (!!) on bool
1515
┌─ /src/warning/wrn.gleam:4:21
1616
1717
4let _ = !!x
18-
^^^
19-
20-
Hint: You can safely remove this.
18+
^^ You can safely remove this.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__warnings__double_unary_integer_literal.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,4 @@ warning: Unnecessary double negation (--) on integer
1010
┌─ /src/warning/wrn.gleam:1:25
1111
1212
1pub fn main() { let _ = --7 }
13-
^^^
14-
15-
Hint: You can safely remove this.
13+
^^ You can safely remove this.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__warnings__double_unary_integer_variable.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,4 @@ warning: Unnecessary double negation (--) on integer
1515
┌─ /src/warning/wrn.gleam:4:21
1616
1717
4let _ = --x
18-
^^^
19-
20-
Hint: You can safely remove this.
18+
^^ You can safely remove this.

compiler-core/src/warning.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,13 @@ Hint: You can safely remove it.
679679
type_::Warning::UnnecessaryDoubleIntNegation { location } => Diagnostic {
680680
title: "Unnecessary double negation (--) on integer".into(),
681681
text: "".into(),
682-
hint: Some("You can safely remove this.".into()),
682+
hint: None,
683683
level: diagnostic::Level::Warning,
684684
location: Some(Location {
685685
src: src.clone(),
686686
path: path.to_path_buf(),
687687
label: diagnostic::Label {
688-
text: None,
688+
text: Some("You can safely remove this.".into()),
689689
span: *location,
690690
},
691691
extra_labels: Vec::new(),
@@ -694,13 +694,13 @@ Hint: You can safely remove it.
694694
type_::Warning::UnnecessaryDoubleBoolNegation { location } => Diagnostic {
695695
title: "Unnecessary double negation (!!) on bool".into(),
696696
text: "".into(),
697-
hint: Some("You can safely remove this.".into()),
697+
hint: None,
698698
level: diagnostic::Level::Warning,
699699
location: Some(Location {
700700
src: src.clone(),
701701
path: path.to_path_buf(),
702702
label: diagnostic::Label {
703-
text: None,
703+
text: Some("You can safely remove this.".into()),
704704
span: *location,
705705
},
706706
extra_labels: Vec::new(),

0 commit comments

Comments
 (0)