3 use syntax::codemap::Spanned;
5 use utils::{get_parent_expr, is_allowed, match_type, paths, span_lint, span_lint_and_sugg, walk_ptrs_ty};
7 /// **What it does:** Checks for string appends of the form `x = x + y` (without
10 /// **Why is this bad?** It's not really bad, but some people think that the
11 /// `.push_str(_)` method is more readable.
13 /// **Known problems:** None.
18 /// let mut x = "Hello".to_owned();
19 /// x = x + ", World";
22 pub STRING_ADD_ASSIGN,
24 "using `x = x + ..` where x is a `String` instead of `push_str()`"
27 /// **What it does:** Checks for all instances of `x + _` where `x` is of type
28 /// `String`, but only if [`string_add_assign`](#string_add_assign) does *not*
31 /// **Why is this bad?** It's not bad in and of itself. However, this particular
32 /// `Add` implementation is asymmetric (the other operand need not be `String`,
33 /// but `x` does), while addition as mathematically defined is symmetric, also
34 /// the `String::push_str(_)` function is a perfectly good replacement.
35 /// Therefore some dislike it and wish not to have it in their code.
37 /// That said, other people think that string addition, having a long tradition
38 /// in other languages is actually fine, which is why we decided to make this
39 /// particular lint `allow` by default.
41 /// **Known problems:** None.
46 /// let x = "Hello".to_owned();
52 "using `x + ..` where x is a `String` instead of `push_str()`"
55 /// **What it does:** Checks for the `as_bytes` method called on string literals
56 /// that contain only ASCII characters.
58 /// **Why is this bad?** Byte string literals (e.g. `b"foo"`) can be used
59 /// instead. They are shorter but less discoverable than `as_bytes()`.
61 /// **Known Problems:** None.
65 /// let bs = "a byte string".as_bytes();
68 pub STRING_LIT_AS_BYTES,
70 "calling `as_bytes` on a string literal instead of using a byte string literal"
73 #[derive(Copy, Clone)]
76 impl LintPass for StringAdd {
77 fn get_lints(&self) -> LintArray {
78 lint_array!(STRING_ADD, STRING_ADD_ASSIGN)
82 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
83 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
84 if let ExprBinary(Spanned { node: BiAdd, .. }, ref left, _) = e.node {
85 if is_string(cx, left) {
86 if !is_allowed(cx, STRING_ADD_ASSIGN, e.id) {
87 let parent = get_parent_expr(cx, e);
88 if let Some(p) = parent {
89 if let ExprAssign(ref target, _) = p.node {
90 // avoid duplicate matches
91 if SpanlessEq::new(cx).eq_expr(target, left) {
101 "you added something to a string. Consider using `String::push_str()` instead",
104 } else if let ExprAssign(ref target, ref src) = e.node {
105 if is_string(cx, target) && is_add(cx, src, target) {
110 "you assigned the result of adding something to this string. Consider using \
111 `String::push_str()` instead",
118 fn is_string(cx: &LateContext, e: &Expr) -> bool {
119 match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(e)), &paths::STRING)
122 fn is_add(cx: &LateContext, src: &Expr, target: &Expr) -> bool {
124 ExprBinary(Spanned { node: BiAdd, .. }, ref left, _) => SpanlessEq::new(cx).eq_expr(target, left),
125 ExprBlock(ref block) => {
126 block.stmts.is_empty()
130 .map_or(false, |expr| is_add(cx, expr, target))
136 #[derive(Copy, Clone)]
137 pub struct StringLitAsBytes;
139 impl LintPass for StringLitAsBytes {
140 fn get_lints(&self) -> LintArray {
141 lint_array!(STRING_LIT_AS_BYTES)
145 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {
146 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
147 use syntax::ast::LitKind;
148 use utils::{in_macro, snippet};
150 if let ExprMethodCall(ref path, _, ref args) = e.node {
151 if path.name == "as_bytes" {
152 if let ExprLit(ref lit) = args[0].node {
153 if let LitKind::Str(ref lit_content, _) = lit.node {
154 if lit_content.as_str().chars().all(|c| c.is_ascii()) && !in_macro(args[0].span) {
159 "calling `as_bytes()` on a string literal",
160 "consider using a byte string literal instead",
161 format!("b{}", snippet(cx, args[0].span, r#""foo""#)),