Skip to content

Commit 439d066

Browse files
committed
Auto merge of rust-lang#114474 - estebank:missing-semi, r=compiler-errors
Detect missing `;` that parses as function call Fix rust-lang#106515.
2 parents faee636 + 8ecb486 commit 439d066

File tree

5 files changed

+169
-13
lines changed

5 files changed

+169
-13
lines changed

compiler/rustc_hir_typeck/src/callee.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -645,18 +645,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
645645
self.typeck_results.borrow().qpath_res(qpath, callee_expr.hir_id)
646646
}
647647
hir::ExprKind::Call(ref inner_callee, _) => {
648-
// If the call spans more than one line and the callee kind is
649-
// itself another `ExprCall`, that's a clue that we might just be
650-
// missing a semicolon (Issue #51055)
651-
let call_is_multiline = self.tcx.sess.source_map().is_multiline(call_expr.span);
652-
if call_is_multiline {
653-
err.span_suggestion(
654-
callee_expr.span.shrink_to_hi(),
655-
"consider using a semicolon here",
656-
";",
657-
Applicability::MaybeIncorrect,
658-
);
659-
}
660648
if let hir::ExprKind::Path(ref inner_qpath) = inner_callee.kind {
661649
inner_callee_path = Some(inner_qpath);
662650
self.typeck_results.borrow().qpath_res(inner_qpath, inner_callee.hir_id)
@@ -668,6 +656,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
668656
};
669657

670658
if !self.maybe_suggest_bad_array_definition(&mut err, call_expr, callee_expr) {
659+
// If the call spans more than one line and the callee kind is
660+
// itself another `ExprCall`, that's a clue that we might just be
661+
// missing a semicolon (#51055, #106515).
662+
let call_is_multiline = self
663+
.tcx
664+
.sess
665+
.source_map()
666+
.is_multiline(call_expr.span.with_lo(callee_expr.span.hi()))
667+
&& call_expr.span.ctxt() == callee_expr.span.ctxt();
668+
if call_is_multiline {
669+
err.span_suggestion(
670+
callee_expr.span.shrink_to_hi(),
671+
"consider using a semicolon here to finish the statement",
672+
";",
673+
Applicability::MaybeIncorrect,
674+
);
675+
}
671676
if let Some((maybe_def, output_ty, _)) = self.extract_callable_info(callee_ty)
672677
&& !self.type_is_sized_modulo_regions(self.param_env, output_ty)
673678
{

tests/ui/suggestions/issue-51055-missing-semicolon-between-call-and-tuple.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn vindictive() -> bool { true }
55
| ----------------------- `vindictive` defined here returns `bool`
66
...
77
LL | vindictive()
8-
| -^^^^^^^^^^^- help: consider using a semicolon here: `;`
8+
| -^^^^^^^^^^^- help: consider using a semicolon here to finish the statement: `;`
99
| _____|
1010
| |
1111
LL | | (1, 2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// run-rustfix
2+
#![allow(dead_code, unused_variables, path_statements)]
3+
fn a() {
4+
let x = 5;
5+
let y = x; //~ ERROR expected function
6+
(); //~ ERROR expected `;`, found `}`
7+
}
8+
9+
fn b() {
10+
let x = 5;
11+
let y = x; //~ ERROR expected function
12+
();
13+
}
14+
fn c() {
15+
let x = 5;
16+
x; //~ ERROR expected function
17+
()
18+
}
19+
fn d() { // ok
20+
let x = || ();
21+
x
22+
()
23+
}
24+
fn e() { // ok
25+
let x = || ();
26+
x
27+
();
28+
}
29+
fn f()
30+
{
31+
let y = 5; //~ ERROR expected function
32+
(); //~ ERROR expected `;`, found `}`
33+
}
34+
fn g() {
35+
5; //~ ERROR expected function
36+
();
37+
}
38+
fn main() {}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// run-rustfix
2+
#![allow(dead_code, unused_variables, path_statements)]
3+
fn a() {
4+
let x = 5;
5+
let y = x //~ ERROR expected function
6+
() //~ ERROR expected `;`, found `}`
7+
}
8+
9+
fn b() {
10+
let x = 5;
11+
let y = x //~ ERROR expected function
12+
();
13+
}
14+
fn c() {
15+
let x = 5;
16+
x //~ ERROR expected function
17+
()
18+
}
19+
fn d() { // ok
20+
let x = || ();
21+
x
22+
()
23+
}
24+
fn e() { // ok
25+
let x = || ();
26+
x
27+
();
28+
}
29+
fn f()
30+
{
31+
let y = 5 //~ ERROR expected function
32+
() //~ ERROR expected `;`, found `}`
33+
}
34+
fn g() {
35+
5 //~ ERROR expected function
36+
();
37+
}
38+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
error: expected `;`, found `}`
2+
--> $DIR/missing-semicolon.rs:6:7
3+
|
4+
LL | ()
5+
| ^ help: add `;` here
6+
LL | }
7+
| - unexpected token
8+
9+
error: expected `;`, found `}`
10+
--> $DIR/missing-semicolon.rs:32:7
11+
|
12+
LL | ()
13+
| ^ help: add `;` here
14+
LL | }
15+
| - unexpected token
16+
17+
error[E0618]: expected function, found `{integer}`
18+
--> $DIR/missing-semicolon.rs:5:13
19+
|
20+
LL | let x = 5;
21+
| - `x` has type `{integer}`
22+
LL | let y = x
23+
| ^- help: consider using a semicolon here to finish the statement: `;`
24+
| _____________|
25+
| |
26+
LL | | ()
27+
| |______- call expression requires function
28+
29+
error[E0618]: expected function, found `{integer}`
30+
--> $DIR/missing-semicolon.rs:11:13
31+
|
32+
LL | let x = 5;
33+
| - `x` has type `{integer}`
34+
LL | let y = x
35+
| ^- help: consider using a semicolon here to finish the statement: `;`
36+
| _____________|
37+
| |
38+
LL | | ();
39+
| |______- call expression requires function
40+
41+
error[E0618]: expected function, found `{integer}`
42+
--> $DIR/missing-semicolon.rs:16:5
43+
|
44+
LL | let x = 5;
45+
| - `x` has type `{integer}`
46+
LL | x
47+
| ^- help: consider using a semicolon here to finish the statement: `;`
48+
| _____|
49+
| |
50+
LL | | ()
51+
| |______- call expression requires function
52+
53+
error[E0618]: expected function, found `{integer}`
54+
--> $DIR/missing-semicolon.rs:31:13
55+
|
56+
LL | let y = 5
57+
| ^- help: consider using a semicolon here to finish the statement: `;`
58+
| _____________|
59+
| |
60+
LL | | ()
61+
| |______- call expression requires function
62+
63+
error[E0618]: expected function, found `{integer}`
64+
--> $DIR/missing-semicolon.rs:35:5
65+
|
66+
LL | 5
67+
| ^- help: consider using a semicolon here to finish the statement: `;`
68+
| _____|
69+
| |
70+
LL | | ();
71+
| |______- call expression requires function
72+
73+
error: aborting due to 7 previous errors
74+
75+
For more information about this error, try `rustc --explain E0618`.

0 commit comments

Comments
 (0)