|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 7 | +// option. This file may not be copied, modified, or distributed |
| 8 | +// except according to those terms. |
| 9 | + |
| 10 | +use crate::rustc::hir::intravisit::FnKind; |
| 11 | +use crate::rustc::hir::{def_id, Body, FnDecl}; |
| 12 | +use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; |
| 13 | +use crate::rustc::mir::{ |
| 14 | + self, traversal, |
| 15 | + visit::{PlaceContext, Visitor}, |
| 16 | + TerminatorKind, |
| 17 | +}; |
| 18 | +use crate::rustc::ty; |
| 19 | +use crate::rustc::{declare_tool_lint, lint_array}; |
| 20 | +use crate::rustc_errors::Applicability; |
| 21 | +use crate::syntax::{ |
| 22 | + ast::NodeId, |
| 23 | + source_map::{BytePos, Span}, |
| 24 | +}; |
| 25 | +use crate::utils::{ |
| 26 | + in_macro, is_copy, match_def_path, match_type, paths, snippet_opt, span_lint_node, span_lint_node_and_then, |
| 27 | + walk_ptrs_ty_depth, |
| 28 | +}; |
| 29 | +use if_chain::if_chain; |
| 30 | +use std::convert::TryFrom; |
| 31 | + |
| 32 | +macro_rules! unwrap_or_continue { |
| 33 | + ($x:expr) => { |
| 34 | + match $x { |
| 35 | + Some(x) => x, |
| 36 | + None => continue, |
| 37 | + } |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +/// **What it does:** Checks for a redudant `clone()` (and its relatives) which clones an owned |
| 42 | +/// value that is going to be dropped without further use. |
| 43 | +/// |
| 44 | +/// **Why is this bad?** It is not always possible for the compiler to eliminate useless |
| 45 | +/// allocations and deallocations generated by redundant `clone()`s. |
| 46 | +/// |
| 47 | +/// **Known problems:** |
| 48 | +/// |
| 49 | +/// * Suggestions made by this lint could require NLL to be enabled. |
| 50 | +/// * False-positive if there is a borrow preventing the value from moving out. |
| 51 | +/// |
| 52 | +/// ```rust |
| 53 | +/// let x = String::new(); |
| 54 | +/// |
| 55 | +/// let y = &x; |
| 56 | +/// |
| 57 | +/// foo(x.clone()); // This lint suggests to remove this `clone()` |
| 58 | +/// ``` |
| 59 | +/// |
| 60 | +/// **Example:** |
| 61 | +/// ```rust |
| 62 | +/// { |
| 63 | +/// let x = Foo::new(); |
| 64 | +/// call(x.clone()); |
| 65 | +/// call(x.clone()); // this can just pass `x` |
| 66 | +/// } |
| 67 | +/// |
| 68 | +/// ["lorem", "ipsum"].join(" ").to_string() |
| 69 | +/// |
| 70 | +/// Path::new("/a/b").join("c").to_path_buf() |
| 71 | +/// ``` |
| 72 | +declare_clippy_lint! { |
| 73 | + pub REDUNDANT_CLONE, |
| 74 | + nursery, |
| 75 | + "`clone()` of an owned value that is going to be dropped immediately" |
| 76 | +} |
| 77 | + |
| 78 | +pub struct RedundantClone; |
| 79 | + |
| 80 | +impl LintPass for RedundantClone { |
| 81 | + fn get_lints(&self) -> LintArray { |
| 82 | + lint_array!(REDUNDANT_CLONE) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone { |
| 87 | + fn check_fn( |
| 88 | + &mut self, |
| 89 | + cx: &LateContext<'a, 'tcx>, |
| 90 | + _: FnKind<'tcx>, |
| 91 | + _: &'tcx FnDecl, |
| 92 | + body: &'tcx Body, |
| 93 | + _: Span, |
| 94 | + _: NodeId, |
| 95 | + ) { |
| 96 | + let def_id = cx.tcx.hir.body_owner_def_id(body.id()); |
| 97 | + let mir = cx.tcx.optimized_mir(def_id); |
| 98 | + |
| 99 | + for (bb, bbdata) in mir.basic_blocks().iter_enumerated() { |
| 100 | + let terminator = bbdata.terminator(); |
| 101 | + |
| 102 | + if in_macro(terminator.source_info.span) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + // Give up on loops |
| 107 | + if terminator.successors().any(|s| *s == bb) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + let (fn_def_id, arg, arg_ty, _) = unwrap_or_continue!(is_call_with_ref_arg(cx, mir, &terminator.kind)); |
| 112 | + |
| 113 | + let from_borrow = match_def_path(cx.tcx, fn_def_id, &paths::CLONE_TRAIT_METHOD) |
| 114 | + || match_def_path(cx.tcx, fn_def_id, &paths::TO_OWNED_METHOD) |
| 115 | + || (match_def_path(cx.tcx, fn_def_id, &paths::TO_STRING_METHOD) |
| 116 | + && match_type(cx, arg_ty, &paths::STRING)); |
| 117 | + |
| 118 | + let from_deref = !from_borrow |
| 119 | + && (match_def_path(cx.tcx, fn_def_id, &paths::PATH_TO_PATH_BUF) |
| 120 | + || match_def_path(cx.tcx, fn_def_id, &paths::OS_STR_TO_OS_STRING)); |
| 121 | + |
| 122 | + if !from_borrow && !from_deref { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + // _1 in MIR `{ _2 = &_1; clone(move _2); }` or `{ _2 = _1; to_path_buf(_2); } (from_deref) |
| 127 | + // In case of `from_deref`, `arg` is already a reference since it is `deref`ed in the previous |
| 128 | + // block. |
| 129 | + let cloned = unwrap_or_continue!(find_stmt_assigns_to(arg, from_borrow, bbdata.statements.iter().rev())); |
| 130 | + |
| 131 | + // _1 in MIR `{ _2 = &_1; _3 = deref(move _2); } -> { _4 = _3; to_path_buf(move _4); }` |
| 132 | + let referent = if from_deref { |
| 133 | + let ps = mir.predecessors_for(bb); |
| 134 | + if ps.len() != 1 { |
| 135 | + continue; |
| 136 | + } |
| 137 | + let pred_terminator = mir[ps[0]].terminator(); |
| 138 | + |
| 139 | + let pred_arg = if_chain! { |
| 140 | + if let Some((pred_fn_def_id, pred_arg, pred_arg_ty, Some(res))) = |
| 141 | + is_call_with_ref_arg(cx, mir, &pred_terminator.kind); |
| 142 | + if *res == mir::Place::Local(cloned); |
| 143 | + if match_def_path(cx.tcx, pred_fn_def_id, &paths::DEREF_TRAIT_METHOD); |
| 144 | + if match_type(cx, pred_arg_ty, &paths::PATH_BUF) |
| 145 | + || match_type(cx, pred_arg_ty, &paths::OS_STRING); |
| 146 | + then { |
| 147 | + pred_arg |
| 148 | + } else { |
| 149 | + continue; |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + unwrap_or_continue!(find_stmt_assigns_to(pred_arg, true, mir[ps[0]].statements.iter().rev())) |
| 154 | + } else { |
| 155 | + cloned |
| 156 | + }; |
| 157 | + |
| 158 | + let used_later = traversal::ReversePostorder::new(&mir, bb).skip(1).any(|(tbb, tdata)| { |
| 159 | + // Give up on loops |
| 160 | + if tdata.terminator().successors().any(|s| *s == bb) { |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + let mut vis = LocalUseVisitor { |
| 165 | + local: referent, |
| 166 | + used_other_than_drop: false, |
| 167 | + }; |
| 168 | + vis.visit_basic_block_data(tbb, tdata); |
| 169 | + vis.used_other_than_drop |
| 170 | + }); |
| 171 | + |
| 172 | + if !used_later { |
| 173 | + let span = terminator.source_info.span; |
| 174 | + let node = if let mir::ClearCrossCrate::Set(scope_local_data) = &mir.source_scope_local_data { |
| 175 | + scope_local_data[terminator.source_info.scope].lint_root |
| 176 | + } else { |
| 177 | + unreachable!() |
| 178 | + }; |
| 179 | + |
| 180 | + if_chain! { |
| 181 | + if let Some(snip) = snippet_opt(cx, span); |
| 182 | + if let Some(dot) = snip.rfind('.'); |
| 183 | + then { |
| 184 | + let sugg_span = span.with_lo( |
| 185 | + span.lo() + BytePos(u32::try_from(dot).unwrap()) |
| 186 | + ); |
| 187 | + |
| 188 | + span_lint_node_and_then(cx, REDUNDANT_CLONE, node, sugg_span, "redundant clone", |db| { |
| 189 | + db.span_suggestion_with_applicability( |
| 190 | + sugg_span, |
| 191 | + "remove this", |
| 192 | + String::new(), |
| 193 | + Applicability::MaybeIncorrect, |
| 194 | + ); |
| 195 | + db.span_note( |
| 196 | + span.with_hi(span.lo() + BytePos(u32::try_from(dot).unwrap())), |
| 197 | + "this value is dropped without further use", |
| 198 | + ); |
| 199 | + }); |
| 200 | + } else { |
| 201 | + span_lint_node(cx, REDUNDANT_CLONE, node, span, "redundant clone"); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +/// If `kind` is `y = func(x: &T)` where `T: !Copy`, returns `(DefId of func, x, T, y)`. |
| 210 | +fn is_call_with_ref_arg<'tcx>( |
| 211 | + cx: &LateContext<'_, 'tcx>, |
| 212 | + mir: &'tcx mir::Mir<'tcx>, |
| 213 | + kind: &'tcx mir::TerminatorKind<'tcx>, |
| 214 | +) -> Option<(def_id::DefId, mir::Local, ty::Ty<'tcx>, Option<&'tcx mir::Place<'tcx>>)> { |
| 215 | + if_chain! { |
| 216 | + if let TerminatorKind::Call { func, args, destination, .. } = kind; |
| 217 | + if args.len() == 1; |
| 218 | + if let mir::Operand::Move(mir::Place::Local(local)) = &args[0]; |
| 219 | + if let ty::FnDef(def_id, _) = func.ty(&*mir, cx.tcx).sty; |
| 220 | + if let (inner_ty, 1) = walk_ptrs_ty_depth(args[0].ty(&*mir, cx.tcx)); |
| 221 | + if !is_copy(cx, inner_ty); |
| 222 | + then { |
| 223 | + Some((def_id, *local, inner_ty, destination.as_ref().map(|(dest, _)| dest))) |
| 224 | + } else { |
| 225 | + None |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +/// Finds the first `to = (&)from`, and returns `Some(from)`. |
| 231 | +fn find_stmt_assigns_to<'a, 'tcx: 'a>( |
| 232 | + to: mir::Local, |
| 233 | + by_ref: bool, |
| 234 | + mut stmts: impl Iterator<Item = &'a mir::Statement<'tcx>>, |
| 235 | +) -> Option<mir::Local> { |
| 236 | + stmts.find_map(|stmt| { |
| 237 | + if let mir::StatementKind::Assign(mir::Place::Local(local), v) = &stmt.kind { |
| 238 | + if *local == to { |
| 239 | + if by_ref { |
| 240 | + if let mir::Rvalue::Ref(_, _, mir::Place::Local(r)) = **v { |
| 241 | + return Some(r); |
| 242 | + } |
| 243 | + } else if let mir::Rvalue::Use(mir::Operand::Copy(mir::Place::Local(r))) = **v { |
| 244 | + return Some(r); |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + None |
| 250 | + }) |
| 251 | +} |
| 252 | + |
| 253 | +struct LocalUseVisitor { |
| 254 | + local: mir::Local, |
| 255 | + used_other_than_drop: bool, |
| 256 | +} |
| 257 | + |
| 258 | +impl<'tcx> mir::visit::Visitor<'tcx> for LocalUseVisitor { |
| 259 | + fn visit_basic_block_data(&mut self, block: mir::BasicBlock, data: &mir::BasicBlockData<'tcx>) { |
| 260 | + let statements = &data.statements; |
| 261 | + for (statement_index, statement) in statements.iter().enumerate() { |
| 262 | + self.visit_statement(block, statement, mir::Location { block, statement_index }); |
| 263 | + |
| 264 | + // Once flagged, skip remaining statements |
| 265 | + if self.used_other_than_drop { |
| 266 | + return; |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + self.visit_terminator( |
| 271 | + block, |
| 272 | + data.terminator(), |
| 273 | + mir::Location { |
| 274 | + block, |
| 275 | + statement_index: statements.len(), |
| 276 | + }, |
| 277 | + ); |
| 278 | + } |
| 279 | + |
| 280 | + fn visit_local(&mut self, local: &mir::Local, ctx: PlaceContext<'tcx>, _: mir::Location) { |
| 281 | + match ctx { |
| 282 | + PlaceContext::Drop | PlaceContext::StorageDead => return, |
| 283 | + _ => {}, |
| 284 | + } |
| 285 | + |
| 286 | + if *local == self.local { |
| 287 | + self.used_other_than_drop = true; |
| 288 | + } |
| 289 | + } |
| 290 | +} |
0 commit comments