From a06fc64f863d94aef0bc8d1df6bc03991923dcf6 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Mon, 23 Sep 2024 21:09:30 +0200 Subject: [PATCH 1/5] add some context to the unused variable warning --- .../expected/unused_variable.res.expected | 14 ++++++++++++++ .../super_errors/fixtures/unused_variable.res | 4 ++++ jscomp/ext/warnings.ml | 3 ++- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 jscomp/build_tests/super_errors/expected/unused_variable.res.expected create mode 100644 jscomp/build_tests/super_errors/fixtures/unused_variable.res diff --git a/jscomp/build_tests/super_errors/expected/unused_variable.res.expected b/jscomp/build_tests/super_errors/expected/unused_variable.res.expected new file mode 100644 index 0000000000..dc6dc0adb4 --- /dev/null +++ b/jscomp/build_tests/super_errors/expected/unused_variable.res.expected @@ -0,0 +1,14 @@ + + Warning number 26 + /.../fixtures/unused_variable.res:2:7 + + 1 │ let x = { + 2 │ let f = 12 + 3 │ 13 + 4 │ } + + unused variable f. + +Fix this by: +- Prepending the variable name with `_` (like `_f`) to ignore that the variable is unused. +- Use the variable somewhere. \ No newline at end of file diff --git a/jscomp/build_tests/super_errors/fixtures/unused_variable.res b/jscomp/build_tests/super_errors/fixtures/unused_variable.res new file mode 100644 index 0000000000..42d2252b3d --- /dev/null +++ b/jscomp/build_tests/super_errors/fixtures/unused_variable.res @@ -0,0 +1,4 @@ +let x = { + let f = 12 + 13 +} diff --git a/jscomp/ext/warnings.ml b/jscomp/ext/warnings.ml index 06233bf643..00e47144a8 100644 --- a/jscomp/ext/warnings.ml +++ b/jscomp/ext/warnings.ml @@ -378,7 +378,8 @@ let message = function | All_clauses_guarded -> "this pattern-matching is not exhaustive.\n\ All clauses in this pattern-matching are guarded." - | Unused_var v | Unused_var_strict v -> "unused variable " ^ v ^ "." + | Unused_var v | Unused_var_strict v -> + Format.sprintf "unused variable %s.\n\nFix this by:\n- Prepending the variable name with `_` (like `_%s`) to ignore that the variable is unused.\n- Use the variable somewhere." v v | Wildcard_arg_to_constant_constr -> "wildcard pattern given as argument to a constant constructor" | Eol_in_string -> From f17de6bbf1382ec16331be1007d63a7859e32e97 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Mon, 23 Sep 2024 21:10:41 +0200 Subject: [PATCH 2/5] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbd7ac2916..e4505bd228 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ #### :nail_care: Polish +- Add some context to error message for unused variables. https://github.com/rescript-lang/rescript-compiler/pull/7050 - Improve error messages for pattern matching on option vs non-option, and vice versa. https://github.com/rescript-lang/rescript-compiler/pull/7035 - Improve bigint literal comparison. https://github.com/rescript-lang/rescript-compiler/pull/7029 - Improve output of `@variadic` bindings. https://github.com/rescript-lang/rescript-compiler/pull/7030 From 87ec0dfe70d67bd2708fa23a9e511224ce70027f Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Tue, 24 Sep 2024 07:11:21 +0200 Subject: [PATCH 3/5] Update jscomp/build_tests/super_errors/expected/unused_variable.res.expected Co-authored-by: Christoph Knittel --- .../super_errors/expected/unused_variable.res.expected | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jscomp/build_tests/super_errors/expected/unused_variable.res.expected b/jscomp/build_tests/super_errors/expected/unused_variable.res.expected index dc6dc0adb4..6799ceaf31 100644 --- a/jscomp/build_tests/super_errors/expected/unused_variable.res.expected +++ b/jscomp/build_tests/super_errors/expected/unused_variable.res.expected @@ -11,4 +11,4 @@ Fix this by: - Prepending the variable name with `_` (like `_f`) to ignore that the variable is unused. -- Use the variable somewhere. \ No newline at end of file +- Using the variable somewhere. \ No newline at end of file From a1961ea4efe28f9951b75ff65b49c35ae7c494ec Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Tue, 24 Sep 2024 08:42:57 +0200 Subject: [PATCH 4/5] Update jscomp/ext/warnings.ml Co-authored-by: Christoph Knittel --- jscomp/ext/warnings.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jscomp/ext/warnings.ml b/jscomp/ext/warnings.ml index 00e47144a8..3ccef7cb6e 100644 --- a/jscomp/ext/warnings.ml +++ b/jscomp/ext/warnings.ml @@ -379,7 +379,7 @@ let message = function "this pattern-matching is not exhaustive.\n\ All clauses in this pattern-matching are guarded." | Unused_var v | Unused_var_strict v -> - Format.sprintf "unused variable %s.\n\nFix this by:\n- Prepending the variable name with `_` (like `_%s`) to ignore that the variable is unused.\n- Use the variable somewhere." v v + Format.sprintf "unused variable %s.\n\nFix this by:\n- Prepending the variable name with `_` (like `_%s`) to ignore that the variable is unused.\n- Using the variable somewhere." v v | Wildcard_arg_to_constant_constr -> "wildcard pattern given as argument to a constant constructor" | Eol_in_string -> From afeb9c3bb8b43796543b945d850a5576bd02bbdd Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Tue, 24 Sep 2024 08:44:11 +0200 Subject: [PATCH 5/5] add suggestion to remove unused variable --- .../super_errors/expected/unused_variable.res.expected | 1 + jscomp/ext/warnings.ml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/jscomp/build_tests/super_errors/expected/unused_variable.res.expected b/jscomp/build_tests/super_errors/expected/unused_variable.res.expected index 6799ceaf31..dfaad847e4 100644 --- a/jscomp/build_tests/super_errors/expected/unused_variable.res.expected +++ b/jscomp/build_tests/super_errors/expected/unused_variable.res.expected @@ -10,5 +10,6 @@ unused variable f. Fix this by: +- Deleting the variable if it's not used anymiore. - Prepending the variable name with `_` (like `_f`) to ignore that the variable is unused. - Using the variable somewhere. \ No newline at end of file diff --git a/jscomp/ext/warnings.ml b/jscomp/ext/warnings.ml index 3ccef7cb6e..0b16549056 100644 --- a/jscomp/ext/warnings.ml +++ b/jscomp/ext/warnings.ml @@ -379,7 +379,7 @@ let message = function "this pattern-matching is not exhaustive.\n\ All clauses in this pattern-matching are guarded." | Unused_var v | Unused_var_strict v -> - Format.sprintf "unused variable %s.\n\nFix this by:\n- Prepending the variable name with `_` (like `_%s`) to ignore that the variable is unused.\n- Using the variable somewhere." v v + Format.sprintf "unused variable %s.\n\nFix this by:\n- Deleting the variable if it's not used anymiore.\n- Prepending the variable name with `_` (like `_%s`) to ignore that the variable is unused.\n- Using the variable somewhere." v v | Wildcard_arg_to_constant_constr -> "wildcard pattern given as argument to a constant constructor" | Eol_in_string ->