2 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
3 use rustc::{declare_lint_pass, declare_tool_lint};
4 use rustc_errors::Applicability;
5 use syntax::source_map::Spanned;
7 use if_chain::if_chain;
9 use crate::utils::SpanlessEq;
10 use crate::utils::{get_parent_expr, is_allowed, match_type, paths, span_lint, span_lint_and_sugg, walk_ptrs_ty};
12 declare_clippy_lint! {
13 /// **What it does:** Checks for string appends of the form `x = x + y` (without
16 /// **Why is this bad?** It's not really bad, but some people think that the
17 /// `.push_str(_)` method is more readable.
19 /// **Known problems:** None.
24 /// let mut x = "Hello".to_owned();
25 /// x = x + ", World";
27 pub STRING_ADD_ASSIGN,
29 "using `x = x + ..` where x is a `String` instead of `push_str()`"
32 declare_clippy_lint! {
33 /// **What it does:** Checks for all instances of `x + _` where `x` is of type
34 /// `String`, but only if [`string_add_assign`](#string_add_assign) does *not*
37 /// **Why is this bad?** It's not bad in and of itself. However, this particular
38 /// `Add` implementation is asymmetric (the other operand need not be `String`,
39 /// but `x` does), while addition as mathematically defined is symmetric, also
40 /// the `String::push_str(_)` function is a perfectly good replacement.
41 /// Therefore, some dislike it and wish not to have it in their code.
43 /// That said, other people think that string addition, having a long tradition
44 /// in other languages is actually fine, which is why we decided to make this
45 /// particular lint `allow` by default.
47 /// **Known problems:** None.
52 /// let x = "Hello".to_owned();
57 "using `x + ..` where x is a `String` instead of `push_str()`"
60 declare_clippy_lint! {
61 /// **What it does:** Checks for the `as_bytes` method called on string literals
62 /// that contain only ASCII characters.
64 /// **Why is this bad?** Byte string literals (e.g., `b"foo"`) can be used
65 /// instead. They are shorter but less discoverable than `as_bytes()`.
67 /// **Known Problems:** None.
71 /// let bs = "a byte string".as_bytes();
73 pub STRING_LIT_AS_BYTES,
75 "calling `as_bytes` on a string literal instead of using a byte string literal"
78 declare_lint_pass!(StringAdd => [STRING_ADD, STRING_ADD_ASSIGN]);
80 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
81 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
82 if let ExprKind::Binary(
84 node: BinOpKind::Add, ..
90 if is_string(cx, left) {
91 if !is_allowed(cx, STRING_ADD_ASSIGN, e.hir_id) {
92 let parent = get_parent_expr(cx, e);
93 if let Some(p) = parent {
94 if let ExprKind::Assign(ref target, _) = p.kind {
95 // avoid duplicate matches
96 if SpanlessEq::new(cx).eq_expr(target, left) {
106 "you added something to a string. Consider using `String::push_str()` instead",
109 } else if let ExprKind::Assign(ref target, ref src) = e.kind {
110 if is_string(cx, target) && is_add(cx, src, target) {
115 "you assigned the result of adding something to this string. Consider using \
116 `String::push_str()` instead",
123 fn is_string(cx: &LateContext<'_, '_>, e: &Expr) -> bool {
124 match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(e)), &paths::STRING)
127 fn is_add(cx: &LateContext<'_, '_>, src: &Expr, target: &Expr) -> bool {
131 node: BinOpKind::Add, ..
135 ) => SpanlessEq::new(cx).eq_expr(target, left),
136 ExprKind::Block(ref block, _) => {
137 block.stmts.is_empty() && block.expr.as_ref().map_or(false, |expr| is_add(cx, expr, target))
143 // Max length a b"foo" string can take
144 const MAX_LENGTH_BYTE_STRING_LIT: usize = 32;
146 declare_lint_pass!(StringLitAsBytes => [STRING_LIT_AS_BYTES]);
148 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {
149 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
150 use crate::utils::{snippet, snippet_with_applicability};
151 use syntax::ast::LitKind;
154 if let ExprKind::MethodCall(path, _, args) = &e.kind;
155 if path.ident.name == sym!(as_bytes);
156 if let ExprKind::Lit(lit) = &args[0].kind;
157 if let LitKind::Str(lit_content, _) = &lit.node;
159 let callsite = snippet(cx, args[0].span.source_callsite(), r#""foo""#);
160 let mut applicability = Applicability::MachineApplicable;
161 if callsite.starts_with("include_str!") {
166 "calling `as_bytes()` on `include_str!(..)`",
167 "consider using `include_bytes!(..)` instead",
168 snippet_with_applicability(cx, args[0].span, r#""foo""#, &mut applicability).replacen(
175 } else if lit_content.as_str().is_ascii()
176 && lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT
177 && !args[0].span.from_expansion()
183 "calling `as_bytes()` on a string literal",
184 "consider using a byte string literal instead",
187 snippet_with_applicability(cx, args[0].span, r#""foo""#, &mut applicability)