Skip to content

Commit a1829bb

Browse files
authored
Rollup merge of rust-lang#100163 - TaKO8Ki:remove-unnecessary-string-search, r=wesleywiser
Refactor: remove an unnecessary string search
2 parents f03ce30 + f6d42aa commit a1829bb

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};
@@ -2398,37 +2397,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23982397
expr,
23992398
Some(span),
24002399
);
2400+
} else if let ty::RawPtr(ty_and_mut) = expr_t.kind()
2401+
&& let ty::Adt(adt_def, _) = ty_and_mut.ty.kind()
2402+
&& let ExprKind::Field(base_expr, _) = expr.kind
2403+
&& adt_def.variants().len() == 1
2404+
&& adt_def
2405+
.variants()
2406+
.iter()
2407+
.next()
2408+
.unwrap()
2409+
.fields
2410+
.iter()
2411+
.any(|f| f.ident(self.tcx) == field)
2412+
{
2413+
err.multipart_suggestion(
2414+
"to access the field, dereference first",
2415+
vec![
2416+
(base_expr.span.shrink_to_lo(), "(*".to_string()),
2417+
(base_expr.span.shrink_to_hi(), ")".to_string()),
2418+
],
2419+
Applicability::MaybeIncorrect,
2420+
);
24012421
} else {
2402-
let mut found = false;
2403-
2404-
if let ty::RawPtr(ty_and_mut) = expr_t.kind()
2405-
&& let ty::Adt(adt_def, _) = ty_and_mut.ty.kind()
2406-
{
2407-
if adt_def.variants().len() == 1
2408-
&& adt_def
2409-
.variants()
2410-
.iter()
2411-
.next()
2412-
.unwrap()
2413-
.fields
2414-
.iter()
2415-
.any(|f| f.ident(self.tcx) == field)
2416-
{
2417-
if let Some(dot_loc) = expr_snippet.rfind('.') {
2418-
found = true;
2419-
err.span_suggestion(
2420-
expr.span.with_hi(expr.span.lo() + BytePos::from_usize(dot_loc)),
2421-
"to access the field, dereference first",
2422-
format!("(*{})", &expr_snippet[0..dot_loc]),
2423-
Applicability::MaybeIncorrect,
2424-
);
2425-
}
2426-
}
2427-
}
2428-
2429-
if !found {
2430-
err.help("methods are immutable and cannot be assigned to");
2431-
}
2422+
err.help("methods are immutable and cannot be assigned to");
24322423
}
24332424

24342425
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)