From 251c3eefd15bb35cad053e9526768ca949f6efea Mon Sep 17 00:00:00 2001 From: mcarton Date: Mon, 14 Mar 2016 22:03:30 +0100 Subject: [PATCH] Use `span_suggestion` in `STRING_LIT_AS_BYTES` --- src/strings.rs | 19 +++++++++++++------ tests/compile-fail/strings.rs | 8 +++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/strings.rs b/src/strings.rs index dac5ac9f6bd6..aa9cdcce50cb 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -8,7 +8,7 @@ use rustc_front::hir::*; use syntax::codemap::Spanned; use utils::STRING_PATH; use utils::SpanlessEq; -use utils::{match_type, span_lint, walk_ptrs_ty, get_parent_expr}; +use utils::{match_type, span_lint, span_lint_and_then, walk_ptrs_ty, get_parent_expr}; /// **What it does:** This lint matches code of the form `x = x + y` (without `let`!). /// @@ -141,11 +141,18 @@ impl LateLintPass for StringLitAsBytes { if let ExprLit(ref lit) = args[0].node { if let LitKind::Str(ref lit_content, _) = lit.node { if lit_content.chars().all(|c| c.is_ascii()) && !in_macro(cx, args[0].span) { - let msg = format!("calling `as_bytes()` on a string literal. \ - Consider using a byte string literal instead: \ - `b{}`", - snippet(cx, args[0].span, r#""foo""#)); - span_lint(cx, STRING_LIT_AS_BYTES, e.span, &msg); + span_lint_and_then(cx, + STRING_LIT_AS_BYTES, + e.span, + "calling `as_bytes()` on a string literal", + |db| { + let sugg = format!("b{}", + snippet(cx, args[0].span, r#""foo""#)); + db.span_suggestion(e.span, + "consider using a byte string literal instead", + sugg); + }); + } } } diff --git a/tests/compile-fail/strings.rs b/tests/compile-fail/strings.rs index 7ed93737ffae..656349ba6218 100644 --- a/tests/compile-fail/strings.rs +++ b/tests/compile-fail/strings.rs @@ -47,9 +47,15 @@ fn both() { #[allow(dead_code, unused_variables)] #[deny(string_lit_as_bytes)] fn str_lit_as_bytes() { - let bs = "hello there".as_bytes(); //~ERROR calling `as_bytes()` + let bs = "hello there".as_bytes(); + //~^ERROR calling `as_bytes()` + //~|HELP byte string literal + //~|SUGGESTION b"hello there" + // no warning, because this cannot be written as a byte string literal: let ubs = "☃".as_bytes(); + + let strify = stringify!(foobar).as_bytes(); } fn main() {