Skip to content

Commit f6d42aa

Browse files
committed
remove an unnecessary str::rfind
1 parent 1f5d8d4 commit f6d42aa

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

compiler/rustc_typeck/src/check/expr.rs

+22-31
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ use rustc_span::hygiene::DesugaringKind;
5050
use rustc_span::lev_distance::find_best_match_for_name;
5151
use rustc_span::source_map::{Span, Spanned};
5252
use rustc_span::symbol::{kw, sym, Ident, Symbol};
53-
use rustc_span::{BytePos, Pos};
5453
use rustc_target::spec::abi::Abi::RustIntrinsic;
5554
use rustc_trait_selection::infer::InferCtxtExt;
5655
use rustc_trait_selection::traits::{self, ObligationCauseCode};
@@ -2391,37 +2390,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23912390
expr,
23922391
Some(span),
23932392
);
2393+
} else if let ty::RawPtr(ty_and_mut) = expr_t.kind()
2394+
&& let ty::Adt(adt_def, _) = ty_and_mut.ty.kind()
2395+
&& let ExprKind::Field(base_expr, _) = expr.kind
2396+
&& adt_def.variants().len() == 1
2397+
&& adt_def
2398+
.variants()
2399+
.iter()
2400+
.next()
2401+
.unwrap()
2402+
.fields
2403+
.iter()
2404+
.any(|f| f.ident(self.tcx) == field)
2405+
{
2406+
err.multipart_suggestion(
2407+
"to access the field, dereference first",
2408+
vec![
2409+
(base_expr.span.shrink_to_lo(), "(*".to_string()),
2410+
(base_expr.span.shrink_to_hi(), ")".to_string()),
2411+
],
2412+
Applicability::MaybeIncorrect,
2413+
);
23942414
} else {
2395-
let mut found = false;
2396-
2397-
if let ty::RawPtr(ty_and_mut) = expr_t.kind()
2398-
&& let ty::Adt(adt_def, _) = ty_and_mut.ty.kind()
2399-
{
2400-
if adt_def.variants().len() == 1
2401-
&& adt_def
2402-
.variants()
2403-
.iter()
2404-
.next()
2405-
.unwrap()
2406-
.fields
2407-
.iter()
2408-
.any(|f| f.ident(self.tcx) == field)
2409-
{
2410-
if let Some(dot_loc) = expr_snippet.rfind('.') {
2411-
found = true;
2412-
err.span_suggestion(
2413-
expr.span.with_hi(expr.span.lo() + BytePos::from_usize(dot_loc)),
2414-
"to access the field, dereference first",
2415-
format!("(*{})", &expr_snippet[0..dot_loc]),
2416-
Applicability::MaybeIncorrect,
2417-
);
2418-
}
2419-
}
2420-
}
2421-
2422-
if !found {
2423-
err.help("methods are immutable and cannot be assigned to");
2424-
}
2415+
err.help("methods are immutable and cannot be assigned to");
24252416
}
24262417

24272418
err.emit();

src/test/ui/typeck/issue-91210-ptr-method.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ error[E0615]: attempted to take value of method `read` on type `*mut Foo`
22
--> $DIR/issue-91210-ptr-method.rs:10:7
33
|
44
LL | x.read = 4;
5-
| - ^^^^ method, not a field
6-
| |
7-
| help: to access the field, dereference first: `(*x)`
5+
| ^^^^ method, not a field
6+
|
7+
help: to access the field, dereference first
8+
|
9+
LL | (*x).read = 4;
10+
| ++ +
811

912
error: aborting due to previous error
1013

0 commit comments

Comments
 (0)