|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::{def_path_res, paths}; |
| 3 | +use rustc_ast::tokenstream::{TokenStream, TokenTree}; |
| 4 | +use rustc_ast::{AttrStyle, DelimArgs}; |
| 5 | +use rustc_hir::def::Res; |
| 6 | +use rustc_hir::def_id::LocalDefId; |
| 7 | +use rustc_hir::{ |
| 8 | + AttrArgs, AttrItem, AttrPath, Attribute, HirId, Impl, Item, ItemKind, Path, QPath, TraitRef, Ty, TyKind, |
| 9 | +}; |
| 10 | +use rustc_lint::{LateContext, LateLintPass}; |
| 11 | +use rustc_lint_defs::declare_tool_lint; |
| 12 | +use rustc_middle::ty::TyCtxt; |
| 13 | +use rustc_session::declare_lint_pass; |
| 14 | +use rustc_span::sym; |
| 15 | +use std::sync::OnceLock; |
| 16 | + |
| 17 | +declare_tool_lint! { |
| 18 | + /// ### What it does |
| 19 | + /// Checks for structs or enums that derive `serde::Deserialize` and that |
| 20 | + /// do not have a `#[serde(deny_unknown_fields)]` attribute. |
| 21 | + /// |
| 22 | + /// ### Why is this bad? |
| 23 | + /// If the struct or enum is used in [`clippy_config::conf::Conf`] and a |
| 24 | + /// user inserts an unknown field by mistake, the user's error will be |
| 25 | + /// silently ignored. |
| 26 | + /// |
| 27 | + /// ### Example |
| 28 | + /// ```rust |
| 29 | + /// #[derive(Deserialize)] |
| 30 | + /// pub struct DisallowedPath { |
| 31 | + /// path: String, |
| 32 | + /// reason: Option<String>, |
| 33 | + /// replacement: Option<String>, |
| 34 | + /// } |
| 35 | + /// ``` |
| 36 | + /// |
| 37 | + /// Use instead: |
| 38 | + /// ```rust |
| 39 | + /// #[derive(Deserialize)] |
| 40 | + /// #[serde(deny_unknown_fields)] |
| 41 | + /// pub struct DisallowedPath { |
| 42 | + /// path: String, |
| 43 | + /// reason: Option<String>, |
| 44 | + /// replacement: Option<String>, |
| 45 | + /// } |
| 46 | + /// ``` |
| 47 | + pub clippy::DERIVE_DESERIALIZE_ALLOWING_UNKNOWN, |
| 48 | + Allow, |
| 49 | + "`#[derive(serde::Deserialize)]` without `#[serde(deny_unknown_fields)]`", |
| 50 | + report_in_external_macro: true |
| 51 | +} |
| 52 | + |
| 53 | +declare_lint_pass!(DeriveDeserializeAllowingUnknown => [DERIVE_DESERIALIZE_ALLOWING_UNKNOWN]); |
| 54 | + |
| 55 | +impl<'tcx> LateLintPass<'tcx> for DeriveDeserializeAllowingUnknown { |
| 56 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { |
| 57 | + // Is this an `impl` (of a certain form)? |
| 58 | + let ItemKind::Impl(Impl { |
| 59 | + of_trait: Some(TraitRef { |
| 60 | + path: Path { res, .. }, .. |
| 61 | + }), |
| 62 | + self_ty: |
| 63 | + Ty { |
| 64 | + kind: |
| 65 | + TyKind::Path(QPath::Resolved( |
| 66 | + None, |
| 67 | + Path { |
| 68 | + res: Res::Def(_, self_ty_def_id), |
| 69 | + .. |
| 70 | + }, |
| 71 | + )), |
| 72 | + .. |
| 73 | + }, |
| 74 | + .. |
| 75 | + }) = item.kind |
| 76 | + else { |
| 77 | + return; |
| 78 | + }; |
| 79 | + |
| 80 | + // Is it an `impl` of the trait `serde::Deserialize`? |
| 81 | + if !is_serde_deserialize_res(cx.tcx, res) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + // Is it derived? |
| 86 | + if !cx.tcx.has_attr(item.owner_id, sym::automatically_derived) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Is `self_ty` local? |
| 91 | + let Some(local_def_id) = self_ty_def_id.as_local() else { |
| 92 | + return; |
| 93 | + }; |
| 94 | + |
| 95 | + // Does `self_ty` have a variant with named fields? |
| 96 | + if !has_variant_with_named_fields(cx.tcx, local_def_id) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + let hir_id = cx.tcx.local_def_id_to_hir_id(local_def_id); |
| 101 | + |
| 102 | + // Does `self_ty` have `#[serde(deny_unknown_fields)]`? |
| 103 | + if let Some(tokens) = find_serde_attr_item(cx.tcx, hir_id) |
| 104 | + && tokens.iter().any(is_deny_unknown_fields_token) |
| 105 | + { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + span_lint( |
| 110 | + cx, |
| 111 | + DERIVE_DESERIALIZE_ALLOWING_UNKNOWN, |
| 112 | + item.span, |
| 113 | + "`#[derive(serde::Deserialize)]` without `#[serde(deny_unknown_fields)]`", |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +fn is_serde_deserialize_res(tcx: TyCtxt<'_>, res: &Res) -> bool { |
| 119 | + static SERDE_DESERIALIZE_RESES: OnceLock<Vec<Res>> = OnceLock::new(); |
| 120 | + |
| 121 | + let serde_deserialize_reses = SERDE_DESERIALIZE_RESES.get_or_init(|| def_path_res(tcx, &paths::SERDE_DESERIALIZE)); |
| 122 | + |
| 123 | + serde_deserialize_reses.contains(res) |
| 124 | +} |
| 125 | + |
| 126 | +// Determines whether `def_id` corresponds to an ADT with at least one variant with named fields. A |
| 127 | +// variant has named fields if its `ctor` field is `None`. |
| 128 | +fn has_variant_with_named_fields(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { |
| 129 | + let ty = tcx.type_of(def_id).skip_binder(); |
| 130 | + |
| 131 | + let rustc_middle::ty::Adt(adt_def, _) = ty.kind() else { |
| 132 | + return false; |
| 133 | + }; |
| 134 | + |
| 135 | + adt_def.variants().iter().any(|variant_def| variant_def.ctor.is_none()) |
| 136 | +} |
| 137 | + |
| 138 | +fn find_serde_attr_item(tcx: TyCtxt<'_>, hir_id: HirId) -> Option<&TokenStream> { |
| 139 | + tcx.hir_attrs(hir_id).iter().find_map(|attribute| { |
| 140 | + if let Attribute::Unparsed(attr_item) = attribute |
| 141 | + && let AttrItem { |
| 142 | + path: AttrPath { segments, .. }, |
| 143 | + args: AttrArgs::Delimited(DelimArgs { tokens, .. }), |
| 144 | + style: AttrStyle::Outer, |
| 145 | + .. |
| 146 | + } = &**attr_item |
| 147 | + && segments.len() == 1 |
| 148 | + && segments[0].as_str() == "serde" |
| 149 | + { |
| 150 | + Some(tokens) |
| 151 | + } else { |
| 152 | + None |
| 153 | + } |
| 154 | + }) |
| 155 | +} |
| 156 | + |
| 157 | +fn is_deny_unknown_fields_token(tt: &TokenTree) -> bool { |
| 158 | + if let TokenTree::Token(token, _) = tt |
| 159 | + && token |
| 160 | + .ident() |
| 161 | + .is_some_and(|(token, _)| token.as_str() == "deny_unknown_fields") |
| 162 | + { |
| 163 | + true |
| 164 | + } else { |
| 165 | + false |
| 166 | + } |
| 167 | +} |
0 commit comments