Skip to content

Commit

Permalink
Move suggestion message to label
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscerie committed Oct 19, 2023
1 parent f6f0196 commit 0acb630
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 140 deletions.
13 changes: 7 additions & 6 deletions selene-lib/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,21 @@ impl Diagnostic {
suggestion: Option<String>,
applicability: Applicability,
) -> Self {
let notes = if let Some(ref suggestion_str) = suggestion {
vec![format!("try: `{}`", suggestion_str)]
} else {
Vec::new()
let mut label = primary_label;
if let Some(ref suggestion_str) = suggestion {
if label.message.is_none() {
label = Label::new_with_message(label.range, format!("try: `{}`", suggestion_str));
}
};

Self {
code,
message,
primary_label,
primary_label: label,
suggestion,
applicability,
notes,

notes: Vec::new(),
secondary_labels: Vec::new(),
}
}
Expand Down
12 changes: 3 additions & 9 deletions selene-lib/tests/lints/almost_swapped/almost_swapped.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@ error[almost_swapped]: this looks like you are trying to swap `a` and `b`
3 │ ╭ a = b
4 │ │ b = a
│ ╰─────^
= try: `a, b = b, a`
│ ╰─────^ try: `a, b = b, a`

error[almost_swapped]: this looks like you are trying to swap `t[1]` and `t[2]`
┌─ almost_swapped.lua:8:1
8 │ ╭ t[1] = t[2]
9 │ │ t[2] = t[1]
│ ╰──────────^
= try: `t[1], t[2] = t[2], t[1]`
│ ╰──────────^ try: `t[1], t[2] = t[2], t[1]`

error[almost_swapped]: this looks like you are trying to swap `a` and `b`
┌─ almost_swapped.lua:15:1
15 │ ╭ a = b
16 │ │ b = a
│ ╰─────^
= try: `a, b = b, a`
│ ╰─────^ try: `a, b = b, a`

Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ error[bad_string_escape]: single quotes do not have to be escaped when inside do
┌─ lua51_string_escapes.lua:11:12
11 │ local x = "\'\""
│ ^^
= try: `'`
│ ^^ try: `'`

error[bad_string_escape]: double quotes do not have to be escaped when inside single quoted strings
┌─ lua51_string_escapes.lua:13:12
13 │ local x = '\"\''
│ ^^
= try: `"`
│ ^^ try: `"`

error[bad_string_escape]: string escape sequence doesn't exist
┌─ lua51_string_escapes.lua:20:14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ error[bad_string_escape]: single quotes do not have to be escaped when inside do
┌─ roblox_string_escapes.lua:11:12
11 │ local x = "\'\""
│ ^^
= try: `'`
│ ^^ try: `'`

error[bad_string_escape]: double quotes do not have to be escaped when inside single quoted strings
┌─ roblox_string_escapes.lua:13:12
13 │ local x = '\"\''
│ ^^
= try: `"`
│ ^^ try: `"`

error[bad_string_escape]: string escape sequence is malformed
┌─ roblox_string_escapes.lua:22:15
Expand Down
8 changes: 2 additions & 6 deletions selene-lib/tests/lints/compare_nan/compare_nan_if.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ error[compare_nan]: comparing things to nan directly is not allowed
┌─ compare_nan_if.lua:1:4
1 │ if x == 0/0 then
│ ^^^^^^^^
= try: `x ~= x`
│ ^^^^^^^^ try: `x ~= x`

error[compare_nan]: comparing things to nan directly is not allowed
┌─ compare_nan_if.lua:4:4
4 │ if x ~= 0/0 then
│ ^^^^^^^^
= try: `x == x`
│ ^^^^^^^^ try: `x == x`

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ error[compare_nan]: comparing things to nan directly is not allowed
┌─ compare_nan_variables.lua:6:11
6 │ local _ = x ~= 0/0
│ ^^^^^^^^
= try: `x == x`
│ ^^^^^^^^ try: `x == x`

error[compare_nan]: comparing things to nan directly is not allowed
┌─ compare_nan_variables.lua:7:11
7 │ local _ = x == 0/0
│ ^^^^^^^^
= try: `x ~= x`
│ ^^^^^^^^ try: `x ~= x`

Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,25 @@ error[constant_table_comparison]: comparing to a constant table will always fail
┌─ constant_table_comparison.lua:4:7
4 │ print(x == {})
│ ^^^^^^^
= try: `next(x) == nil`
│ ^^^^^^^ try: `next(x) == nil`

error[constant_table_comparison]: comparing to a constant table will always fail
┌─ constant_table_comparison.lua:5:7
5 │ print({} == x)
│ ^^^^^^^
= try: `next(x) == nil`
│ ^^^^^^^ try: `next(x) == nil`

error[constant_table_comparison]: comparing to a constant table will always fail
┌─ constant_table_comparison.lua:6:7
6 │ print(x ~= {})
│ ^^^^^^^
= try: `next(x) ~= nil`
│ ^^^^^^^ try: `next(x) ~= nil`

error[constant_table_comparison]: comparing to a constant table will always fail
┌─ constant_table_comparison.lua:7:7
7 │ print({} ~= x)
│ ^^^^^^^
= try: `next(x) ~= nil`
│ ^^^^^^^ try: `next(x) ~= nil`

error[constant_table_comparison]: comparing to a constant table will always fail
┌─ constant_table_comparison.lua:9:7
Expand All @@ -64,7 +56,5 @@ error[constant_table_comparison]: comparing to a constant table will always fail
┌─ constant_table_comparison.lua:14:2
14 │ t == {}
│ ^^^^^^^
= try: `next(t) == nil`
│ ^^^^^^^ try: `next(t) == nil`

8 changes: 2 additions & 6 deletions selene-lib/tests/lints/divide_by_zero/divide_by_zero.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ error[divide_by_zero]: dividing by zero is not readable
┌─ divide_by_zero.lua:2:11
2 │ local _ = 5 / 0
│ ^^^^^
= try: `math.huge()`
│ ^^^^^ try: `math.huge()`

error[divide_by_zero]: dividing by zero is not readable
┌─ divide_by_zero.lua:4:11
4 │ local _ = x / 0
│ ^^^^^
= try: `math.huge()`
│ ^^^^^ try: `math.huge()`

36 changes: 9 additions & 27 deletions selene-lib/tests/lints/empty_loop/empty_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ error[empty_loop]: empty loop block
9 │ ╭ for _ = 1, 10 do
10 │ │ -- Should warn
11 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:13:1
Expand All @@ -16,9 +14,7 @@ error[empty_loop]: empty loop block
15 │ │ Should warn
16 │ │ ]]
17 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:19:1
Expand All @@ -28,61 +24,47 @@ error[empty_loop]: empty loop block
21 │ │
22 │ │
23 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:29:1
29 │ ╭ for _ in pairs({}) do
30 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:36:1
36 │ ╭ for _ in ipairs({}) do
37 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:43:1
43 │ ╭ for _ in {} do
44 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:50:1
50 │ ╭ for _ in a() do
51 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:57:1
57 │ ╭ while true do
58 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:64:1
64 │ ╭ repeat
65 │ │ until true
│ ╰──────────^
= try: ``
│ ╰──────────^ try: ``

28 changes: 7 additions & 21 deletions selene-lib/tests/lints/empty_loop/empty_loop_comments.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,47 @@ error[empty_loop]: empty loop block
21 │ │
22 │ │
23 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:29:1
29 │ ╭ for _ in pairs({}) do
30 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:36:1
36 │ ╭ for _ in ipairs({}) do
37 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:43:1
43 │ ╭ for _ in {} do
44 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:50:1
50 │ ╭ for _ in a() do
51 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:57:1
57 │ ╭ while true do
58 │ │ end
│ ╰───^
= try: ``
│ ╰───^ try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:64:1
64 │ ╭ repeat
65 │ │ until true
│ ╰──────────^
= try: ``
│ ╰──────────^ try: ``

Loading

0 comments on commit 0acb630

Please sign in to comment.