diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2018-07-01 20:03:35 -0700 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2018-07-01 20:03:35 -0700 |
| commit | e89db3030d322ca7cb20d13df5578b60b0c8a942 (patch) | |
| tree | d2bec2f95e69b2d6a41ea9925fe86f311294d2b4 /src | |
| parent | c697a56d01c650f65b4ca4a264db1ea3573d0565 (diff) | |
| download | rust-e89db3030d322ca7cb20d13df5578b60b0c8a942.tar.gz rust-e89db3030d322ca7cb20d13df5578b60b0c8a942.zip | |
Do not suggest changes to str literal if it isn't one
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc_typeck/check/demand.rs | 16 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-48364.rs | 16 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-48364.stderr | 12 |
3 files changed, 38 insertions, 6 deletions
diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 08d8dd2e498..aee64ad3b55 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -264,9 +264,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { (&ty::TyStr, &ty::TySlice(arr)) if arr == self.tcx.types.u8 => { if let hir::ExprLit(_) = expr.node { if let Ok(src) = cm.span_to_snippet(sp) { - return Some((sp, - "consider removing the leading `b`", - src[1..].to_string())); + if src.starts_with("b\"") { + return Some((sp, + "consider removing the leading `b`", + src[1..].to_string())); + } } } }, @@ -274,9 +276,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { (&ty::TySlice(arr), &ty::TyStr) if arr == self.tcx.types.u8 => { if let hir::ExprLit(_) = expr.node { if let Ok(src) = cm.span_to_snippet(sp) { - return Some((sp, - "consider adding a leading `b`", - format!("b{}", src))); + if src.starts_with("\"") { + return Some((sp, + "consider adding a leading `b`", + format!("b{}", src))); + } } } } diff --git a/src/test/ui/suggestions/issue-48364.rs b/src/test/ui/suggestions/issue-48364.rs new file mode 100644 index 00000000000..82cb722a656 --- /dev/null +++ b/src/test/ui/suggestions/issue-48364.rs @@ -0,0 +1,16 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn foo() -> bool { + b"".starts_with(stringify!(foo)) + //~^ ERROR mismatched types +} + +fn main() {} diff --git a/src/test/ui/suggestions/issue-48364.stderr b/src/test/ui/suggestions/issue-48364.stderr new file mode 100644 index 00000000000..b420654a32d --- /dev/null +++ b/src/test/ui/suggestions/issue-48364.stderr @@ -0,0 +1,12 @@ +error[E0308]: mismatched types + --> $DIR/issue-48364.rs:12:21 + | +LL | b"".starts_with(stringify!(foo)) + | ^^^^^^^^^^^^^^^ expected slice, found str + | + = note: expected type `&[u8]` + found type `&'static str` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. |
