about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorFlorian Hartwig <florian.j.hartwig@gmail.com>2016-01-19 19:14:49 +0100
committerFlorian Hartwig <florian.j.hartwig@gmail.com>2016-01-19 19:17:50 +0100
commit01eda52cb5c2d3a787b2c6e81f3c699370525387 (patch)
tree3a7507a3eaacf1a001757f0a7dd34e25821b266a /src
parenta0496f08caf15c53ace0687051a52fbcc3473c25 (diff)
downloadrust-01eda52cb5c2d3a787b2c6e81f3c699370525387.tar.gz
rust-01eda52cb5c2d3a787b2c6e81f3c699370525387.zip
Add lint for "string literal".as_bytes()
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs2
-rw-r--r--src/strings.rs49
2 files changed, 51 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 6c60d403e1c..76d9426b25a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -135,6 +135,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
     reg.register_late_lint_pass(box misc::UsedUnderscoreBinding);
     reg.register_late_lint_pass(box array_indexing::ArrayIndexing);
     reg.register_late_lint_pass(box panic::PanicPass);
+    reg.register_late_lint_pass(box strings::StringLitAsBytes);
 
 
     reg.register_lint_group("clippy_pedantic", vec![
@@ -225,6 +226,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
         ranges::RANGE_ZIP_WITH_LEN,
         returns::LET_AND_RETURN,
         returns::NEEDLESS_RETURN,
+        strings::STRING_LIT_AS_BYTES,
         temporary_assignment::TEMPORARY_ASSIGNMENT,
         transmute::USELESS_TRANSMUTE,
         types::BOX_VEC,
diff --git a/src/strings.rs b/src/strings.rs
index 55d1a0acf68..c32289f84fd 100644
--- a/src/strings.rs
+++ b/src/strings.rs
@@ -48,6 +48,22 @@ declare_lint! {
     "using `x + ..` where x is a `String`; suggests using `push_str()` instead"
 }
 
+/// **What it does:** This lint matches the `as_bytes` method called on string
+/// literals that contain only ascii characters. It is `Warn` by default.
+///
+/// **Why is this bad?** Byte string literals (e.g. `b"foo"`) can be used instead. They are shorter but less discoverable than `as_bytes()`.
+///
+/// **Example:**
+///
+/// ```
+/// let bs = "a byte string".as_bytes();
+/// ```
+declare_lint! {
+    pub STRING_LIT_AS_BYTES,
+    Warn,
+    "calling `as_bytes` on a string literal; suggests using a byte string literal instead"
+}
+
 #[derive(Copy, Clone)]
 pub struct StringAdd;
 
@@ -104,3 +120,36 @@ fn is_add(cx: &LateContext, src: &Expr, target: &Expr) -> bool {
         _ => false,
     }
 }
+
+#[derive(Copy, Clone)]
+pub struct StringLitAsBytes;
+
+impl LintPass for StringLitAsBytes {
+    fn get_lints(&self) -> LintArray {
+        lint_array!(STRING_LIT_AS_BYTES)
+    }
+}
+
+impl LateLintPass for StringLitAsBytes {
+    fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
+        use std::ascii::AsciiExt;
+        use syntax::ast::Lit_::LitStr;
+        use utils::snippet;
+
+        if let ExprMethodCall(ref name, _, ref args) = e.node {
+            if name.node.as_str() == "as_bytes" {
+                if let ExprLit(ref lit) = args[0].node {
+                    if let LitStr(ref lit_content, _) = lit.node {
+                        if lit_content.chars().all(|c| c.is_ascii()) {
+                            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);
+                        }
+                    }
+                }
+            }
+        }
+    }
+}