Skip to content

Commit 5195132

Browse files
authored
Rollup merge of rust-lang#51978 - estebank:issue-48364, r=oli-obk
Do not suggest changes to str literal if it isn't one Fix rust-lang#48364.
2 parents 47eee24 + e89db30 commit 5195132

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/librustc_typeck/check/demand.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
264264
(&ty::TyStr, &ty::TySlice(arr)) if arr == self.tcx.types.u8 => {
265265
if let hir::ExprLit(_) = expr.node {
266266
if let Ok(src) = cm.span_to_snippet(sp) {
267-
return Some((sp,
268-
"consider removing the leading `b`",
269-
src[1..].to_string()));
267+
if src.starts_with("b\"") {
268+
return Some((sp,
269+
"consider removing the leading `b`",
270+
src[1..].to_string()));
271+
}
270272
}
271273
}
272274
},
273275
(&ty::TyArray(arr, _), &ty::TyStr) |
274276
(&ty::TySlice(arr), &ty::TyStr) if arr == self.tcx.types.u8 => {
275277
if let hir::ExprLit(_) = expr.node {
276278
if let Ok(src) = cm.span_to_snippet(sp) {
277-
return Some((sp,
278-
"consider adding a leading `b`",
279-
format!("b{}", src)));
279+
if src.starts_with("\"") {
280+
return Some((sp,
281+
"consider adding a leading `b`",
282+
format!("b{}", src)));
283+
}
280284
}
281285
}
282286
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo() -> bool {
12+
b"".starts_with(stringify!(foo))
13+
//~^ ERROR mismatched types
14+
}
15+
16+
fn main() {}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-48364.rs:12:21
3+
|
4+
LL | b"".starts_with(stringify!(foo))
5+
| ^^^^^^^^^^^^^^^ expected slice, found str
6+
|
7+
= note: expected type `&[u8]`
8+
found type `&'static str`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)