X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fstrings.rs;h=f84566ef707a8e41dbc8a2b00275d7d55c07aeb6;hb=19339334cb4e9c6db5a1f7dced38edcb16707bc7;hp=ffe7c13ec5754454b36d65b62749aae04bb6051f;hpb=05cb0df748dbc14c43dea4d627d68721a52861c6;p=rust.git diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs index ffe7c13ec57..f84566ef707 100644 --- a/clippy_lints/src/strings.rs +++ b/clippy_lints/src/strings.rs @@ -1,14 +1,14 @@ -use rustc::lint::in_external_macro; use rustc_errors::Applicability; -use rustc_hir::*; +use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_middle::lint::in_external_macro; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Spanned; use if_chain::if_chain; 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 crate::utils::{get_parent_expr, is_allowed, is_type_diagnostic_item, 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 @@ -24,6 +24,10 @@ /// ```rust /// let mut x = "Hello".to_owned(); /// x = x + ", World"; + /// + /// // More readable + /// x += ", World"; + /// x.push_str(", World"); /// ``` pub STRING_ADD_ASSIGN, pedantic, @@ -69,7 +73,11 @@ /// /// **Example:** /// ```rust + /// // Bad /// let bs = "a byte string".as_bytes(); + /// + /// // Good + /// let bs = b"a byte string"; /// ``` pub STRING_LIT_AS_BYTES, style, @@ -126,7 +134,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { } fn is_string(cx: &LateContext<'_, '_>, e: &Expr<'_>) -> bool { - match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(e)), &paths::STRING) + is_type_diagnostic_item(cx, walk_ptrs_ty(cx.tables.expr_ty(e)), sym!(string_type)) } fn is_add(cx: &LateContext<'_, '_>, src: &Expr<'_>, target: &Expr<'_>) -> bool { @@ -153,7 +161,7 @@ fn is_add(cx: &LateContext<'_, '_>, src: &Expr<'_>, target: &Expr<'_>) -> bool { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { use crate::utils::{snippet, snippet_with_applicability}; - use syntax::ast::LitKind; + use rustc_ast::ast::LitKind; if_chain! { if let ExprKind::MethodCall(path, _, args) = &e.kind;