Skip to content

Commit af6b300

Browse files
committed
fix: verify self is callee part of call expr parent
1 parent d1ed52b commit af6b300

File tree

3 files changed

+1195
-6
lines changed

3 files changed

+1195
-6
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,13 +1592,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15921592
&& let hir::Node::Stmt(&hir::Stmt { kind: hir::StmtKind::Semi(parent), .. })
15931593
| hir::Node::Expr(parent) = self.tcx.parent_hir_node(path_expr.hir_id)
15941594
{
1595-
let replacement_span =
1596-
if let hir::ExprKind::Call(..) | hir::ExprKind::Struct(..) = parent.kind {
1597-
// We want to replace the parts that need to go, like `()` and `{}`.
1595+
// We want to also replace variant constructor part like `()` and `{}`.
1596+
let replacement_span = match parent.kind {
1597+
hir::ExprKind::Call(callee, ..) if callee.hir_id == path_expr.hir_id => {
15981598
span.with_hi(parent.span.hi())
1599-
} else {
1600-
span
1601-
};
1599+
}
1600+
hir::ExprKind::Struct(..) => span.with_hi(parent.span.hi()),
1601+
_ => span,
1602+
};
16021603
match (variant.ctor, parent.kind) {
16031604
(None, hir::ExprKind::Struct(..)) => {
16041605
// We want a struct and we have a struct. We won't suggest changing
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Regression test for #146586
2+
3+
//@ only-linux
4+
//@ compile-flags: --error-format=human --color=always
5+
6+
enum Enum {
7+
Unit,
8+
Tuple(i32),
9+
Struct { x: i32 },
10+
}
11+
12+
fn foo(_: Enum) {}
13+
14+
fn main() {
15+
foo(Enum::Unit);
16+
foo(Enum::Tuple);
17+
foo(Enum::Struct); // Highlight is wrong and suggestion is malformed
18+
foo(Enum::Unit());
19+
foo(Enum::Tuple());
20+
foo(Enum::Struct());
21+
foo(Enum::Unit {});
22+
foo(Enum::Tuple {});
23+
foo(Enum::Struct {});
24+
foo(Enum::Unit(0));
25+
foo(Enum::Tuple(0));
26+
foo(Enum::Struct(0));
27+
foo(Enum::Unit { x: 0 });
28+
foo(Enum::Tuple { x: 0 });
29+
foo(Enum::Struct { x: 0 });
30+
foo(Enum::Unit(0, 0));
31+
foo(Enum::Tuple(0, 0));
32+
foo(Enum::Struct(0, 0));
33+
foo(Enum::Unit { x: 0, y: 0 });
34+
foo(Enum::Tuple { x: 0, y: 0 });
35+
foo(Enum::Struct { x: 0, y: 0 });
36+
foo(Enum::unit); // Suggestion was malformed
37+
foo(Enum::tuple); // Suggestion could be enhanced
38+
foo(Enum::r#struct); // Suggestion was malformed
39+
foo(Enum::unit());
40+
foo(Enum::tuple());
41+
foo(Enum::r#struct());
42+
foo(Enum::unit {}); // Suggestion could be enhanced
43+
foo(Enum::tuple {}); // Suggestion could be enhanced
44+
foo(Enum::r#struct {}); // Suggestion could be enhanced
45+
foo(Enum::unit(0));
46+
foo(Enum::tuple(0));
47+
foo(Enum::r#struct(0));
48+
foo(Enum::unit { x: 0 }); // Suggestion could be enhanced
49+
foo(Enum::tuple { x: 0 }); // Suggestion could be enhanced
50+
foo(Enum::r#struct { x: 0 });
51+
foo(Enum::unit(0, 0));
52+
foo(Enum::tuple(0, 0));
53+
foo(Enum::r#struct(0, 0));
54+
foo(Enum::unit { x: 0, y: 0 }); // Suggestion could be enhanced
55+
foo(Enum::tuple { x: 0, y: 0 }); // Suggestion could be enhanced
56+
foo(Enum::r#struct { x: 0, y: 0 }); // Suggestion could be enhanced
57+
}

0 commit comments

Comments
 (0)