Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove as_str if the type is already &str #64739

Merged
merged 6 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,10 @@ impl<'tcx> TyS<'tcx> {
#[inline]
pub fn is_bool(&self) -> bool { self.kind == Bool }

/// Returns `true` if this type is a `str`.
#[inline]
pub fn is_str(&self) -> bool { self.kind == Str }

#[inline]
pub fn is_param(&self, index: u32) -> bool {
match self.kind {
Expand Down
9 changes: 8 additions & 1 deletion src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

if let Some(lev_candidate) = lev_candidate {
if item_name.as_str() == "as_str" && actual.peel_refs().is_str() {
// FIXME: the span is not quite correct, it should point to ".as_str()" instead
// of just "as_str".
err.span_label(
span,
guanqun marked this conversation as resolved.
Show resolved Hide resolved
"try removing `as_str`"
);
} else if let Some(lev_candidate) = lev_candidate {
let def_kind = lev_candidate.def_kind();
err.span_suggestion(
span,
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/suggestions/remove-as_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fn foo1(s: &str) {
s.as_str();
//~^ ERROR no method named `as_str` found for type `&str` in the current scope
}

fn foo2<'a>(s: &'a str) {
s.as_str();
//~^ ERROR no method named `as_str` found for type `&'a str` in the current scope
}

fn foo3(s: &mut str) {
s.as_str();
//~^ ERROR no method named `as_str` found for type `&mut str` in the current scope
}

fn foo4(s: &&str) {
s.as_str();
//~^ ERROR no method named `as_str` found for type `&&str` in the current scope
}

fn main() {}
27 changes: 27 additions & 0 deletions src/test/ui/suggestions/remove-as_str.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0599]: no method named `as_str` found for type `&str` in the current scope
--> $DIR/remove-as_str.rs:2:7
|
LL | s.as_str();
| ^^^^^^ try removing `as_str`

error[E0599]: no method named `as_str` found for type `&'a str` in the current scope
--> $DIR/remove-as_str.rs:7:7
|
LL | s.as_str();
| ^^^^^^ try removing `as_str`

error[E0599]: no method named `as_str` found for type `&mut str` in the current scope
--> $DIR/remove-as_str.rs:12:7
|
LL | s.as_str();
| ^^^^^^ try removing `as_str`

error[E0599]: no method named `as_str` found for type `&&str` in the current scope
--> $DIR/remove-as_str.rs:17:7
|
LL | s.as_str();
| ^^^^^^ try removing `as_str`

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0599`.