Report anonymous functions as "this function" rather than symbol name

Closes #2453

Thanks for the assist @chris-packett
This commit is contained in:
Ayaz Hafiz 2022-08-10 15:26:52 -07:00
parent 1f45d13c67
commit fdb79da5a8
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
4 changed files with 45 additions and 6 deletions

View file

@ -136,12 +136,16 @@ pub fn can_problem<'b>(
title = UNKNOWN_GENERATES_WITH.to_string();
severity = Severity::RuntimeError;
}
Problem::UnusedArgument(closure_symbol, argument_symbol, region) => {
Problem::UnusedArgument(closure_symbol, is_anonymous, argument_symbol, region) => {
let line = "\". Adding an underscore at the start of a variable name is a way of saying that the variable is not used.";
doc = alloc.stack([
alloc.concat([
alloc.symbol_unqualified(closure_symbol),
if is_anonymous {
alloc.reflow("This function")
} else {
alloc.symbol_unqualified(closure_symbol)
},
alloc.reflow(" doesn't use "),
alloc.symbol_unqualified(argument_symbol),
alloc.text("."),
@ -153,7 +157,11 @@ pub fn can_problem<'b>(
alloc.reflow(", then you can just remove it. However, if you really do need "),
alloc.symbol_unqualified(argument_symbol),
alloc.reflow(" as an argument of "),
alloc.symbol_unqualified(closure_symbol),
if is_anonymous {
alloc.reflow("this function")
} else {
alloc.symbol_unqualified(closure_symbol)
},
alloc.reflow(", prefix it with an underscore, like this: \"_"),
alloc.symbol_unqualified(argument_symbol),
alloc.reflow(line),

View file

@ -10307,4 +10307,26 @@ All branches in an `if` must have the same type!
@r###"
"###
);
test_report!(
anonymous_function_does_not_use_param,
indoc!(
r#"
(\x -> 5) 1
"#
),
@r###"
UNUSED ARGUMENT /code/proj/Main.roc
This function doesn't use `x`.
4 (\x -> 5) 1
^
If you don't need `x`, then you can just remove it. However, if you
really do need `x` as an argument of this function, prefix it with an
underscore, like this: "_`x`". Adding an underscore at the start of a
variable name is a way of saying that the variable is not used.
"###
);
}