]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/strings.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / strings.rs
index 5414a1ac0de9b5e5c5466907af6ce0c313ee9a1b..cd23722bef4f9a8e0d9e6892d4dd8e977456981c 100644 (file)
@@ -1,81 +1,73 @@
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// 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.
-
-use crate::utils::SpanlessEq;
-use crate::utils::{get_parent_expr, is_allowed, match_type, paths, span_lint, span_lint_and_sugg, walk_ptrs_ty};
 use rustc::hir::*;
 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
 use rustc::{declare_tool_lint, lint_array};
 use rustc_errors::Applicability;
 use syntax::source_map::Spanned;
 
-/// **What it does:** Checks for string appends of the form `x = x + y` (without
-/// `let`!).
-///
-/// **Why is this bad?** It's not really bad, but some people think that the
-/// `.push_str(_)` method is more readable.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-///
-/// ```rust
-/// let mut x = "Hello".to_owned();
-/// x = x + ", World";
-/// ```
+use crate::utils::SpanlessEq;
+use crate::utils::{get_parent_expr, is_allowed, match_type, paths, span_lint, span_lint_and_sugg, walk_ptrs_ty};
+
 declare_clippy_lint! {
+    /// **What it does:** Checks for string appends of the form `x = x + y` (without
+    /// `let`!).
+    ///
+    /// **Why is this bad?** It's not really bad, but some people think that the
+    /// `.push_str(_)` method is more readable.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    ///
+    /// ```rust
+    /// let mut x = "Hello".to_owned();
+    /// x = x + ", World";
+    /// ```
     pub STRING_ADD_ASSIGN,
     pedantic,
     "using `x = x + ..` where x is a `String` instead of `push_str()`"
 }
 
-/// **What it does:** Checks for all instances of `x + _` where `x` is of type
-/// `String`, but only if [`string_add_assign`](#string_add_assign) does *not*
-/// match.
-///
-/// **Why is this bad?** It's not bad in and of itself. However, this particular
-/// `Add` implementation is asymmetric (the other operand need not be `String`,
-/// but `x` does), while addition as mathematically defined is symmetric, also
-/// the `String::push_str(_)` function is a perfectly good replacement.
-/// Therefore some dislike it and wish not to have it in their code.
-///
-/// That said, other people think that string addition, having a long tradition
-/// in other languages is actually fine, which is why we decided to make this
-/// particular lint `allow` by default.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-///
-/// ```rust
-/// let x = "Hello".to_owned();
-/// x + ", World"
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for all instances of `x + _` where `x` is of type
+    /// `String`, but only if [`string_add_assign`](#string_add_assign) does *not*
+    /// match.
+    ///
+    /// **Why is this bad?** It's not bad in and of itself. However, this particular
+    /// `Add` implementation is asymmetric (the other operand need not be `String`,
+    /// but `x` does), while addition as mathematically defined is symmetric, also
+    /// the `String::push_str(_)` function is a perfectly good replacement.
+    /// Therefore, some dislike it and wish not to have it in their code.
+    ///
+    /// That said, other people think that string addition, having a long tradition
+    /// in other languages is actually fine, which is why we decided to make this
+    /// particular lint `allow` by default.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    ///
+    /// ```rust
+    /// let x = "Hello".to_owned();
+    /// x + ", World"
+    /// ```
     pub STRING_ADD,
     restriction,
     "using `x + ..` where x is a `String` instead of `push_str()`"
 }
 
-/// **What it does:** Checks for the `as_bytes` method called on string literals
-/// that contain only ASCII characters.
-///
-/// **Why is this bad?** Byte string literals (e.g. `b"foo"`) can be used
-/// instead. They are shorter but less discoverable than `as_bytes()`.
-///
-/// **Known Problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// let bs = "a byte string".as_bytes();
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for the `as_bytes` method called on string literals
+    /// that contain only ASCII characters.
+    ///
+    /// **Why is this bad?** Byte string literals (e.g., `b"foo"`) can be used
+    /// instead. They are shorter but less discoverable than `as_bytes()`.
+    ///
+    /// **Known Problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// let bs = "a byte string".as_bytes();
+    /// ```
     pub STRING_LIT_AS_BYTES,
     style,
     "calling `as_bytes` on a string literal instead of using a byte string literal"
@@ -88,6 +80,10 @@ impl LintPass for StringAdd {
     fn get_lints(&self) -> LintArray {
         lint_array!(STRING_ADD, STRING_ADD_ASSIGN)
     }
+
+    fn name(&self) -> &'static str {
+        "StringAdd"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
@@ -101,7 +97,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
         ) = e.node
         {
             if is_string(cx, left) {
-                if !is_allowed(cx, STRING_ADD_ASSIGN, e.id) {
+                if !is_allowed(cx, STRING_ADD_ASSIGN, e.hir_id) {
                     let parent = get_parent_expr(cx, e);
                     if let Some(p) = parent {
                         if let ExprKind::Assign(ref target, _) = p.node {
@@ -160,6 +156,10 @@ impl LintPass for StringLitAsBytes {
     fn get_lints(&self) -> LintArray {
         lint_array!(STRING_LIT_AS_BYTES)
     }
+
+    fn name(&self) -> &'static str {
+        "StringLiteralAsBytes"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {