Skip to content

Commit f92f3c4

Browse files
committed
Auto merge of #64739 - guanqun:remove-as-str, r=estebank
Remove as_str if the type is already &str Fix #62642 r? @estebank do you think the suggestion tip is good enough?
2 parents 4ac4809 + 11c2d43 commit f92f3c4

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

src/librustc/ty/sty.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,10 @@ impl<'tcx> TyS<'tcx> {
17751775
#[inline]
17761776
pub fn is_bool(&self) -> bool { self.kind == Bool }
17771777

1778+
/// Returns `true` if this type is a `str`.
1779+
#[inline]
1780+
pub fn is_str(&self) -> bool { self.kind == Str }
1781+
17781782
#[inline]
17791783
pub fn is_param(&self, index: u32) -> bool {
17801784
match self.kind {

src/librustc_typeck/check/method/suggest.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
518518
}
519519
}
520520

521-
if let Some(lev_candidate) = lev_candidate {
521+
if item_name.as_str() == "as_str" && actual.peel_refs().is_str() {
522+
// FIXME: the span is not quite correct, it should point to ".as_str()" instead
523+
// of just "as_str".
524+
err.span_label(
525+
span,
526+
"try removing `as_str`"
527+
);
528+
} else if let Some(lev_candidate) = lev_candidate {
522529
let def_kind = lev_candidate.def_kind();
523530
err.span_suggestion(
524531
span,
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fn foo1(s: &str) {
2+
s.as_str();
3+
//~^ ERROR no method named `as_str` found for type `&str` in the current scope
4+
}
5+
6+
fn foo2<'a>(s: &'a str) {
7+
s.as_str();
8+
//~^ ERROR no method named `as_str` found for type `&'a str` in the current scope
9+
}
10+
11+
fn foo3(s: &mut str) {
12+
s.as_str();
13+
//~^ ERROR no method named `as_str` found for type `&mut str` in the current scope
14+
}
15+
16+
fn foo4(s: &&str) {
17+
s.as_str();
18+
//~^ ERROR no method named `as_str` found for type `&&str` in the current scope
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0599]: no method named `as_str` found for type `&str` in the current scope
2+
--> $DIR/remove-as_str.rs:2:7
3+
|
4+
LL | s.as_str();
5+
| ^^^^^^ try removing `as_str`
6+
7+
error[E0599]: no method named `as_str` found for type `&'a str` in the current scope
8+
--> $DIR/remove-as_str.rs:7:7
9+
|
10+
LL | s.as_str();
11+
| ^^^^^^ try removing `as_str`
12+
13+
error[E0599]: no method named `as_str` found for type `&mut str` in the current scope
14+
--> $DIR/remove-as_str.rs:12:7
15+
|
16+
LL | s.as_str();
17+
| ^^^^^^ try removing `as_str`
18+
19+
error[E0599]: no method named `as_str` found for type `&&str` in the current scope
20+
--> $DIR/remove-as_str.rs:17:7
21+
|
22+
LL | s.as_str();
23+
| ^^^^^^ try removing `as_str`
24+
25+
error: aborting due to 4 previous errors
26+
27+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)