|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, ImplicitSelfKind}; |
| 5 | +use rustc_lint::LateContext; |
| 6 | +use rustc_middle::ty; |
| 7 | +use rustc_span::Span; |
| 8 | + |
| 9 | +use super::MISSNAMED_GETTERS; |
| 10 | + |
| 11 | +pub fn check_fn( |
| 12 | + cx: &LateContext<'_>, |
| 13 | + kind: FnKind<'_>, |
| 14 | + decl: &FnDecl<'_>, |
| 15 | + body: &Body<'_>, |
| 16 | + _span: Span, |
| 17 | + _hir_id: HirId, |
| 18 | +) { |
| 19 | + let FnKind::Method(ref ident, sig) = kind else { |
| 20 | + return; |
| 21 | + }; |
| 22 | + |
| 23 | + // Takes only &(mut) self |
| 24 | + if decl.inputs.len() != 1 { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + let name = ident.name.as_str(); |
| 29 | + |
| 30 | + let name = match sig.decl.implicit_self { |
| 31 | + ImplicitSelfKind::ImmRef => name, |
| 32 | + ImplicitSelfKind::MutRef => { |
| 33 | + let Some(name) = name.strip_suffix("_mut") else { |
| 34 | + return; |
| 35 | + }; |
| 36 | + name |
| 37 | + }, |
| 38 | + _ => return, |
| 39 | + }; |
| 40 | + |
| 41 | + // Body must be &(mut) <self_data>.name |
| 42 | + // self_data is not neccessarilly self |
| 43 | + let (self_data, used_ident, span) = if_chain! { |
| 44 | + if let ExprKind::Block(block,_) = body.value.kind; |
| 45 | + if block.stmts.is_empty(); |
| 46 | + if let Some(block_expr) = block.expr; |
| 47 | + // replace with while for as many addrof needed |
| 48 | + if let ExprKind::AddrOf(_,_, expr) = block_expr.kind; |
| 49 | + if let ExprKind::Field(self_data, ident) = expr.kind; |
| 50 | + if ident.name.as_str() != name; |
| 51 | + then { |
| 52 | + (self_data,ident,block_expr.span) |
| 53 | + } else { |
| 54 | + return; |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + let ty = cx.typeck_results().expr_ty(self_data); |
| 59 | + |
| 60 | + let def = { |
| 61 | + let mut kind = ty.kind(); |
| 62 | + loop { |
| 63 | + match kind { |
| 64 | + ty::Adt(def, _) => break def, |
| 65 | + ty::Ref(_, ty, _) => kind = ty.kind(), |
| 66 | + // We don't do tuples because the function name cannot be a number |
| 67 | + _ => return, |
| 68 | + } |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + let variants = def.variants(); |
| 73 | + |
| 74 | + // We're accessing a field, so it should be an union or a struct and have one and only one variant |
| 75 | + if variants.len() != 1 { |
| 76 | + if cfg!(debug_assertions) { |
| 77 | + panic!("Struct or union expected to have only one variant"); |
| 78 | + } else { |
| 79 | + // Don't ICE when possible |
| 80 | + return; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + let first = variants.last().unwrap(); |
| 85 | + let fields = &variants[first]; |
| 86 | + |
| 87 | + let mut used_field = None; |
| 88 | + let mut correct_field = None; |
| 89 | + for f in &fields.fields { |
| 90 | + if f.name.as_str() == name { |
| 91 | + correct_field = Some(f); |
| 92 | + } |
| 93 | + if f.name == used_ident.name { |
| 94 | + used_field = Some(f); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + let Some(used_field) = used_field else { |
| 99 | + if cfg!(debug_assertions) { |
| 100 | + panic!("Struct doesn't contain the correct field"); |
| 101 | + } else { |
| 102 | + // Don't ICE when possible |
| 103 | + return; |
| 104 | + } |
| 105 | + }; |
| 106 | + let Some(correct_field) = correct_field else { |
| 107 | + return; |
| 108 | + }; |
| 109 | + |
| 110 | + if cx.tcx.type_of(used_field.did) == cx.tcx.type_of(correct_field.did) { |
| 111 | + let snippet = snippet(cx, span, ".."); |
| 112 | + let sugg = format!("{}{name}", snippet.strip_suffix(used_field.name.as_str()).unwrap()); |
| 113 | + span_lint_and_sugg( |
| 114 | + cx, |
| 115 | + MISSNAMED_GETTERS, |
| 116 | + span, |
| 117 | + "getter function appears to return the wrong field", |
| 118 | + "consider using", |
| 119 | + sugg, |
| 120 | + Applicability::MaybeIncorrect, |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
0 commit comments