summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/methods/unnecessary_join.rs
blob: 087e1e4343b707b4e29a87bcd9012ecd38e6de62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use clippy_utils::{diagnostics::span_lint_and_sugg, ty::is_type_lang_item};
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, LangItem};
use rustc_lint::LateContext;
use rustc_middle::ty::{Ref, Slice};
use rustc_span::Span;

use super::UNNECESSARY_JOIN;

pub(super) fn check<'tcx>(
    cx: &LateContext<'tcx>,
    expr: &'tcx Expr<'tcx>,
    join_self_arg: &'tcx Expr<'tcx>,
    join_arg: &'tcx Expr<'tcx>,
    span: Span,
) {
    let applicability = Applicability::MachineApplicable;
    let collect_output_adjusted_type = cx.typeck_results().expr_ty_adjusted(join_self_arg);
    if_chain! {
        // the turbofish for collect is ::<Vec<String>>
        if let Ref(_, ref_type, _) = collect_output_adjusted_type.kind();
        if let Slice(slice) = ref_type.kind();
        if is_type_lang_item(cx, *slice, LangItem::String);
        // the argument for join is ""
        if let ExprKind::Lit(spanned) = &join_arg.kind;
        if let LitKind::Str(symbol, _) = spanned.node;
        if symbol.is_empty();
        then {
            span_lint_and_sugg(
                cx,
                UNNECESSARY_JOIN,
                span.with_hi(expr.span.hi()),
                r#"called `.collect::<Vec<String>>().join("")` on an iterator"#,
                "try using",
                "collect::<String>()".to_owned(),
                applicability,
            );
        }
    }
}