From 0acb630a900edf26ad1fa71977e2e1502ce99014 Mon Sep 17 00:00:00 2001 From: Christopher Chang <51393127+chriscerie@users.noreply.github.com> Date: Wed, 18 Oct 2023 18:06:04 -0700 Subject: [PATCH] Move suggestion message to label --- selene-lib/src/lints.rs | 13 +++---- .../almost_swapped/almost_swapped.stderr | 12 ++----- .../lua51_string_escapes.stderr | 8 ++--- .../roblox_string_escapes.stderr | 8 ++--- .../lints/compare_nan/compare_nan_if.stderr | 8 ++--- .../compare_nan/compare_nan_variables.stderr | 8 ++--- .../constant_table_comparison.stderr | 20 +++-------- .../divide_by_zero/divide_by_zero.stderr | 8 ++--- .../tests/lints/empty_loop/empty_loop.stderr | 36 +++++-------------- .../empty_loop/empty_loop_comments.stderr | 28 ++++----------- .../parenthese_conditions.stderr | 32 +++++------------ .../lints/standard_library/method_call.stderr | 4 +-- .../unscoped_variables.stderr | 8 ++--- 13 files changed, 53 insertions(+), 140 deletions(-) diff --git a/selene-lib/src/lints.rs b/selene-lib/src/lints.rs index 246fde48..2704ccfa 100644 --- a/selene-lib/src/lints.rs +++ b/selene-lib/src/lints.rs @@ -129,20 +129,21 @@ impl Diagnostic { suggestion: Option, 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(), } } diff --git a/selene-lib/tests/lints/almost_swapped/almost_swapped.stderr b/selene-lib/tests/lints/almost_swapped/almost_swapped.stderr index 56639b2f..5e80fba3 100644 --- a/selene-lib/tests/lints/almost_swapped/almost_swapped.stderr +++ b/selene-lib/tests/lints/almost_swapped/almost_swapped.stderr @@ -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` diff --git a/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.stderr b/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.stderr index 12508462..0de21348 100644 --- a/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.stderr +++ b/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.stderr @@ -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 diff --git a/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.stderr b/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.stderr index 3c4bc975..3a42a730 100644 --- a/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.stderr +++ b/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.stderr @@ -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 diff --git a/selene-lib/tests/lints/compare_nan/compare_nan_if.stderr b/selene-lib/tests/lints/compare_nan/compare_nan_if.stderr index 34d34c6f..b03b390c 100644 --- a/selene-lib/tests/lints/compare_nan/compare_nan_if.stderr +++ b/selene-lib/tests/lints/compare_nan/compare_nan_if.stderr @@ -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` diff --git a/selene-lib/tests/lints/compare_nan/compare_nan_variables.stderr b/selene-lib/tests/lints/compare_nan/compare_nan_variables.stderr index 776c66bb..53248bc8 100644 --- a/selene-lib/tests/lints/compare_nan/compare_nan_variables.stderr +++ b/selene-lib/tests/lints/compare_nan/compare_nan_variables.stderr @@ -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` diff --git a/selene-lib/tests/lints/constant_table_comparison/constant_table_comparison.stderr b/selene-lib/tests/lints/constant_table_comparison/constant_table_comparison.stderr index 9b7c509d..dc027047 100644 --- a/selene-lib/tests/lints/constant_table_comparison/constant_table_comparison.stderr +++ b/selene-lib/tests/lints/constant_table_comparison/constant_table_comparison.stderr @@ -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 @@ -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` diff --git a/selene-lib/tests/lints/divide_by_zero/divide_by_zero.stderr b/selene-lib/tests/lints/divide_by_zero/divide_by_zero.stderr index d20d0485..246657c2 100644 --- a/selene-lib/tests/lints/divide_by_zero/divide_by_zero.stderr +++ b/selene-lib/tests/lints/divide_by_zero/divide_by_zero.stderr @@ -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()` diff --git a/selene-lib/tests/lints/empty_loop/empty_loop.stderr b/selene-lib/tests/lints/empty_loop/empty_loop.stderr index 183daa8f..d06bdde0 100644 --- a/selene-lib/tests/lints/empty_loop/empty_loop.stderr +++ b/selene-lib/tests/lints/empty_loop/empty_loop.stderr @@ -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 @@ -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 @@ -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: `` diff --git a/selene-lib/tests/lints/empty_loop/empty_loop_comments.stderr b/selene-lib/tests/lints/empty_loop/empty_loop_comments.stderr index 405eda83..5c244a27 100644 --- a/selene-lib/tests/lints/empty_loop/empty_loop_comments.stderr +++ b/selene-lib/tests/lints/empty_loop/empty_loop_comments.stderr @@ -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: `` diff --git a/selene-lib/tests/lints/parenthese_conditions/parenthese_conditions.stderr b/selene-lib/tests/lints/parenthese_conditions/parenthese_conditions.stderr index f42e84f6..607683ce 100644 --- a/selene-lib/tests/lints/parenthese_conditions/parenthese_conditions.stderr +++ b/selene-lib/tests/lints/parenthese_conditions/parenthese_conditions.stderr @@ -2,41 +2,31 @@ error[parenthese_conditions]: lua does not require parentheses around conditions ┌─ parenthese_conditions.lua:2:4 │ 2 │ if (x) then end - │ ^^^ - │ - = try: `x` + │ ^^^ try: `x` error[parenthese_conditions]: lua does not require parentheses around conditions ┌─ parenthese_conditions.lua:4:4 │ 4 │ if (x) then - │ ^^^ - │ - = try: `x` + │ ^^^ try: `x` error[parenthese_conditions]: lua does not require parentheses around conditions ┌─ parenthese_conditions.lua:6:8 │ 6 │ elseif (z) then - │ ^^^ - │ - = try: `z` + │ ^^^ try: `z` error[parenthese_conditions]: lua does not require parentheses around conditions ┌─ parenthese_conditions.lua:10:7 │ 10 │ while (true) do end - │ ^^^^^^ - │ - = try: `true` + │ ^^^^^^ try: `true` error[parenthese_conditions]: lua does not require parentheses around conditions ┌─ parenthese_conditions.lua:13:14 │ 13 │ repeat until (false) - │ ^^^^^^^ - │ - = try: `false` + │ ^^^^^^^ try: `false` error[parenthese_conditions]: lua does not require parentheses around conditions ┌─ parenthese_conditions.lua:17:4 @@ -45,17 +35,13 @@ error[parenthese_conditions]: lua does not require parentheses around conditions │ ╭────^ 18 │ │ if (x) then end 19 │ │ end)()) then end - │ ╰───────^ - │ - = try: `(function() - if (x) then end - end)()` + │ ╰───────^ try: `(function() + if (x) then end +end)()` error[parenthese_conditions]: lua does not require parentheses around conditions ┌─ parenthese_conditions.lua:18:8 │ 18 │ if (x) then end - │ ^^^ - │ - = try: `x` + │ ^^^ try: `x` diff --git a/selene-lib/tests/lints/standard_library/method_call.stderr b/selene-lib/tests/lints/standard_library/method_call.stderr index 804b2f80..b74f94a5 100644 --- a/selene-lib/tests/lints/standard_library/method_call.stderr +++ b/selene-lib/tests/lints/standard_library/method_call.stderr @@ -8,9 +8,7 @@ error[incorrect_standard_library_use]: standard library function `foo.bar` is a ┌─ method_call.lua:3:1 │ 3 │ foo.bar() - │ ^^^^^^^^^ - │ - = try: `foo:bar(...)` + │ ^^^^^^^^^ try: `foo:bar(...)` error[incorrect_standard_library_use]: standard library global `foo` does not contain the field `baz` ┌─ method_call.lua:4:1 diff --git a/selene-lib/tests/lints/unscoped_variables/unscoped_variables.stderr b/selene-lib/tests/lints/unscoped_variables/unscoped_variables.stderr index b85d4c38..541568a5 100644 --- a/selene-lib/tests/lints/unscoped_variables/unscoped_variables.stderr +++ b/selene-lib/tests/lints/unscoped_variables/unscoped_variables.stderr @@ -2,15 +2,11 @@ error[unscoped_variables]: `bar` is not declared locally, and will be available ┌─ unscoped_variables.lua:1:1 │ 1 │ bar = true - │ ^^^ - │ - = try: `local bar` + │ ^^^ try: `local bar` error[unscoped_variables]: `baz` is not declared locally, and will be available in every scope ┌─ unscoped_variables.lua:11:5 │ 11 │ baz = 1 - │ ^^^ - │ - = try: `local baz` + │ ^^^ try: `local baz`