1
Fork 0

Rollup merge of #91503 - estebank:call-fn-span, r=michaelwoerister

Tweak "call this function" suggestion to have smaller span
This commit is contained in:
Matthias Krüger 2021-12-07 11:05:03 +01:00 committed by GitHub
commit a8f47dc7aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 21 deletions

View file

@ -492,7 +492,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> bool /* did we suggest to call a function because of missing parentheses? */ { ) -> bool /* did we suggest to call a function because of missing parentheses? */ {
err.span_label(span, ty.to_string()); err.span_label(span, ty.to_string());
if let FnDef(def_id, _) = *ty.kind() { if let FnDef(def_id, _) = *ty.kind() {
let source_map = self.tcx.sess.source_map();
if !self.tcx.has_typeck_results(def_id) { if !self.tcx.has_typeck_results(def_id) {
return false; return false;
} }
@ -517,20 +516,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.lookup_op_method(fn_sig.output(), &[other_ty], Op::Binary(op, is_assign)) .lookup_op_method(fn_sig.output(), &[other_ty], Op::Binary(op, is_assign))
.is_ok() .is_ok()
{ {
if let Ok(snippet) = source_map.span_to_snippet(span) { let (variable_snippet, applicability) = if !fn_sig.inputs().is_empty() {
let (variable_snippet, applicability) = if !fn_sig.inputs().is_empty() { ("( /* arguments */ )".to_string(), Applicability::HasPlaceholders)
(format!("{}( /* arguments */ )", snippet), Applicability::HasPlaceholders) } else {
} else { ("()".to_string(), Applicability::MaybeIncorrect)
(format!("{}()", snippet), Applicability::MaybeIncorrect) };
};
err.span_suggestion( err.span_suggestion_verbose(
span, span.shrink_to_hi(),
"you might have forgotten to call this function", "you might have forgotten to call this function",
variable_snippet, variable_snippet,
applicability, applicability,
); );
}
return true; return true;
} }
} }

View file

@ -9,11 +9,11 @@ LL | let x = f == g;
help: you might have forgotten to call this function help: you might have forgotten to call this function
| |
LL | let x = f() == g; LL | let x = f() == g;
| ~~~ | ++
help: you might have forgotten to call this function help: you might have forgotten to call this function
| |
LL | let x = f == g(); LL | let x = f == g();
| ~~~ | ++
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/fn-compare-mismatch.rs:4:18 --> $DIR/fn-compare-mismatch.rs:4:18

View file

@ -5,7 +5,11 @@ LL | foo > 12;
| --- ^ -- {integer} | --- ^ -- {integer}
| | | |
| fn() -> i32 {foo} | fn() -> i32 {foo}
| help: you might have forgotten to call this function: `foo()` |
help: you might have forgotten to call this function
|
LL | foo() > 12;
| ++
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-59488.rs:14:11 --> $DIR/issue-59488.rs:14:11
@ -23,7 +27,11 @@ LL | bar > 13;
| --- ^ -- {integer} | --- ^ -- {integer}
| | | |
| fn(i64) -> i64 {bar} | fn(i64) -> i64 {bar}
| help: you might have forgotten to call this function: `bar( /* arguments */ )` |
help: you might have forgotten to call this function
|
LL | bar( /* arguments */ ) > 13;
| +++++++++++++++++++
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-59488.rs:18:11 --> $DIR/issue-59488.rs:18:11
@ -45,11 +53,11 @@ LL | foo > foo;
help: you might have forgotten to call this function help: you might have forgotten to call this function
| |
LL | foo() > foo; LL | foo() > foo;
| ~~~~~ | ++
help: you might have forgotten to call this function help: you might have forgotten to call this function
| |
LL | foo > foo(); LL | foo > foo();
| ~~~~~ | ++
error[E0369]: binary operation `>` cannot be applied to type `fn() -> i32 {foo}` error[E0369]: binary operation `>` cannot be applied to type `fn() -> i32 {foo}`
--> $DIR/issue-59488.rs:25:9 --> $DIR/issue-59488.rs:25:9

View file

@ -6,9 +6,12 @@ LL | assert_eq!(a, 0);
| | | |
| fn() -> i32 {a} | fn() -> i32 {a}
| {integer} | {integer}
| help: you might have forgotten to call this function: `*left_val()`
| |
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might have forgotten to call this function
|
LL | if !(*left_val() == *right_val) {
| ++
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-70724-add_type_neq_err_label-unwrap.rs:6:5 --> $DIR/issue-70724-add_type_neq_err_label-unwrap.rs:6:5