From a5d931050e69df1bc07117a2891154a71a06a73e Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Tue, 24 Sep 2019 17:49:25 +0800 Subject: [PATCH 1/6] remove 'as_str' when it's already a str type. fix #62642 --- src/librustc_typeck/check/method/suggest.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 2d4d2e32f23db..e68e3211cade1 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -518,7 +518,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - if let Some(lev_candidate) = lev_candidate { + fn is_str_ref<'tcx>(ty: Ty<'tcx>) -> bool { + match ty.sty { + ty::Str => true, + ty::Ref(_, ty, _) => is_str_ref(&ty), + _ => false, + } + } + if item_name.as_str() == "as_str" && is_str_ref(&actual) { + err.span_suggestion( + span, + "try to remove `as_str`", + String::new(), + Applicability::MaybeIncorrect, + ); + } else if let Some(lev_candidate) = lev_candidate { let def_kind = lev_candidate.def_kind(); err.span_suggestion( span, From bb3c03049dbef28d18a7ca17b5b9d25f7c276b8e Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Tue, 24 Sep 2019 23:02:21 +0800 Subject: [PATCH 2/6] add test files --- src/librustc_typeck/check/method/suggest.rs | 2 +- src/test/ui/suggestions/remove-as_str.rs | 21 +++++++++++++++ src/test/ui/suggestions/remove-as_str.stderr | 27 ++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/suggestions/remove-as_str.rs create mode 100644 src/test/ui/suggestions/remove-as_str.stderr diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index e68e3211cade1..6efa304559210 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -518,7 +518,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - fn is_str_ref<'tcx>(ty: Ty<'tcx>) -> bool { + fn is_str_ref(ty: Ty<'_>) -> bool { match ty.sty { ty::Str => true, ty::Ref(_, ty, _) => is_str_ref(&ty), diff --git a/src/test/ui/suggestions/remove-as_str.rs b/src/test/ui/suggestions/remove-as_str.rs new file mode 100644 index 0000000000000..d10300b48ba1e --- /dev/null +++ b/src/test/ui/suggestions/remove-as_str.rs @@ -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() {} diff --git a/src/test/ui/suggestions/remove-as_str.stderr b/src/test/ui/suggestions/remove-as_str.stderr new file mode 100644 index 0000000000000..090a359fa68de --- /dev/null +++ b/src/test/ui/suggestions/remove-as_str.stderr @@ -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(); + | ^^^^^^ help: try to remove `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(); + | ^^^^^^ help: try to remove `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(); + | ^^^^^^ help: try to remove `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(); + | ^^^^^^ help: try to remove `as_str` + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0599`. From 4cb86c908989087dca7d3ad2b5c9029c62aaf852 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Wed, 25 Sep 2019 12:31:52 +0800 Subject: [PATCH 3/6] comment fixes --- src/librustc/ty/sty.rs | 4 ++++ src/librustc_typeck/check/method/suggest.rs | 11 ++--------- src/test/ui/suggestions/remove-as_str.stderr | 8 ++++---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index e105c44d09ae5..dfd8168985c02 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -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.sty == Str } + #[inline] pub fn is_param(&self, index: u32) -> bool { match self.kind { diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 6efa304559210..89055628342b1 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -518,17 +518,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - fn is_str_ref(ty: Ty<'_>) -> bool { - match ty.sty { - ty::Str => true, - ty::Ref(_, ty, _) => is_str_ref(&ty), - _ => false, - } - } - if item_name.as_str() == "as_str" && is_str_ref(&actual) { + if item_name.as_str() == "as_str" && actual.peel_refs().is_str() { err.span_suggestion( span, - "try to remove `as_str`", + "try removing `as_str`", String::new(), Applicability::MaybeIncorrect, ); diff --git a/src/test/ui/suggestions/remove-as_str.stderr b/src/test/ui/suggestions/remove-as_str.stderr index 090a359fa68de..47196aa2c8ea0 100644 --- a/src/test/ui/suggestions/remove-as_str.stderr +++ b/src/test/ui/suggestions/remove-as_str.stderr @@ -2,25 +2,25 @@ error[E0599]: no method named `as_str` found for type `&str` in the current scop --> $DIR/remove-as_str.rs:2:7 | LL | s.as_str(); - | ^^^^^^ help: try to remove `as_str` + | ^^^^^^ help: 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(); - | ^^^^^^ help: try to remove `as_str` + | ^^^^^^ help: 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(); - | ^^^^^^ help: try to remove `as_str` + | ^^^^^^ help: 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(); - | ^^^^^^ help: try to remove `as_str` + | ^^^^^^ help: try removing `as_str` error: aborting due to 4 previous errors From 5abc5e9b3df85ec6fa22e67e0ea624cd225d2007 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Tue, 1 Oct 2019 15:22:52 +0800 Subject: [PATCH 4/6] add FIXME and use 'span_label' --- src/librustc_typeck/check/method/suggest.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 89055628342b1..e9a8a52857ab3 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -519,11 +519,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } if item_name.as_str() == "as_str" && actual.peel_refs().is_str() { - err.span_suggestion( + // FIXME: the span is not quite correct, it should point to ".as_str()" instead + // of just "as_str". + err.span_label( span, - "try removing `as_str`", - String::new(), - Applicability::MaybeIncorrect, + "try removing `as_str`" ); } else if let Some(lev_candidate) = lev_candidate { let def_kind = lev_candidate.def_kind(); From 51482d0b75de535243032265269562ffa7538c46 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Tue, 1 Oct 2019 15:29:09 +0800 Subject: [PATCH 5/6] fix unit tests --- src/test/ui/suggestions/remove-as_str.stderr | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/ui/suggestions/remove-as_str.stderr b/src/test/ui/suggestions/remove-as_str.stderr index 47196aa2c8ea0..2e8b72ebd4f6d 100644 --- a/src/test/ui/suggestions/remove-as_str.stderr +++ b/src/test/ui/suggestions/remove-as_str.stderr @@ -2,25 +2,25 @@ error[E0599]: no method named `as_str` found for type `&str` in the current scop --> $DIR/remove-as_str.rs:2:7 | LL | s.as_str(); - | ^^^^^^ help: try removing `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(); - | ^^^^^^ help: try removing `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(); - | ^^^^^^ help: try removing `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(); - | ^^^^^^ help: try removing `as_str` + | ^^^^^^ try removing `as_str` error: aborting due to 4 previous errors From 11c2d43485e92a00d86b3a5bc1655165b6b8371b Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Wed, 2 Oct 2019 12:09:30 +0800 Subject: [PATCH 6/6] typo fix in the code --- src/librustc/ty/sty.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index dfd8168985c02..ebeb26be23df6 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -1778,7 +1778,7 @@ impl<'tcx> TyS<'tcx> { /// Returns `true` if this type is a `str`. #[inline] - pub fn is_str(&self) -> bool { self.sty == Str } + pub fn is_str(&self) -> bool { self.kind == Str } #[inline] pub fn is_param(&self, index: u32) -> bool {