|
| 1 | +use clippy_utils::diagnostics::{span_lint_and_help, span_lint_hir}; |
| 2 | +use clippy_utils::str_utils::to_snake_case; |
| 3 | +use rustc_hir::{FieldDef, ItemKind, VariantData}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 6 | +use rustc_span::Span; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// ### What it does |
| 10 | + /// Detects struct fields that are prefixed or suffixed |
| 11 | + /// by the same characters or the name of the struct itself. |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// Information common to all struct fields is better represented in the struct name. |
| 15 | + /// |
| 16 | + /// ### Limitations |
| 17 | + /// Characters with no casing will be considered when comparing prefixes/suffixes |
| 18 | + /// This applies to numbers and non-ascii characters without casing |
| 19 | + /// e.g. `Foo1` and `foo2` is considered to have different prefixes |
| 20 | + /// (the prefixes are `foo1` and `foo2` respectively), as also `bar螃`, `bar蟹` |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```rust |
| 24 | + /// struct Cake { |
| 25 | + /// cake_sugar: u8, |
| 26 | + /// cake_flour: u8, |
| 27 | + /// cake_eggs: u8 |
| 28 | + /// } |
| 29 | + /// ``` |
| 30 | + /// Use instead: |
| 31 | + /// ```rust |
| 32 | + /// struct Cake { |
| 33 | + /// sugar: u8, |
| 34 | + /// flour: u8, |
| 35 | + /// eggs: u8 |
| 36 | + /// } |
| 37 | + /// ``` |
| 38 | + #[clippy::version = "1.74.0"] |
| 39 | + pub STRUCT_FIELD_NAMES, |
| 40 | + style, |
| 41 | + "structs where all fields share a prefix/postfix or contain the name of the struct" |
| 42 | +} |
| 43 | + |
| 44 | +pub struct StructFields { |
| 45 | + threshold: u64, |
| 46 | + avoid_breaking_exported_api: bool, |
| 47 | +} |
| 48 | + |
| 49 | +impl StructFields { |
| 50 | + pub fn new(threshold: u64, avoid_breaking_exported_api: bool) -> StructFields { |
| 51 | + StructFields { |
| 52 | + threshold, |
| 53 | + avoid_breaking_exported_api, |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl_lint_pass!(StructFields => [STRUCT_FIELD_NAMES]); |
| 59 | + |
| 60 | +fn is_snake_case(name: &str) -> bool { |
| 61 | + name.chars().all(|c| !c.is_uppercase() || c == '_') |
| 62 | +} |
| 63 | + |
| 64 | +fn check_fields(cx: &LateContext<'_>, threshold: u64, fields: &[FieldDef<'_>], item_name: &str, item_span: Span) { |
| 65 | + if (fields.len() as u64) < threshold { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + for field in fields { |
| 70 | + if is_snake_case(field.ident.name.as_str()) { |
| 71 | + check_struct_start(cx, item_name, field); |
| 72 | + check_struct_end(cx, item_name, field); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + let first_field = match fields.get(0) { |
| 77 | + Some(field) => field.ident.name.as_str(), |
| 78 | + None => return, |
| 79 | + }; |
| 80 | + let mut pre: Vec<&str> = first_field.split('_').collect(); |
| 81 | + let mut post = pre.clone(); |
| 82 | + post.reverse(); |
| 83 | + for field in fields { |
| 84 | + let field_split: Vec<&str> = field.ident.name.as_str().split('_').collect(); |
| 85 | + if field_split.len() == 1 { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + pre = pre |
| 90 | + .iter() |
| 91 | + .zip(field_split.iter()) |
| 92 | + .take_while(|(a, b)| a == b) |
| 93 | + .map(|e| *e.0) |
| 94 | + .collect(); |
| 95 | + post = post |
| 96 | + .iter() |
| 97 | + .zip(field_split.iter().rev()) |
| 98 | + .take_while(|(a, b)| a == b) |
| 99 | + .map(|e| *e.0) |
| 100 | + .collect(); |
| 101 | + } |
| 102 | + let prefix = pre.join("_"); |
| 103 | + post.reverse(); |
| 104 | + let postfix = post.join("_"); |
| 105 | + if fields.len() > 1 { |
| 106 | + let (what, value) = match (prefix.is_empty(), postfix.is_empty()) { |
| 107 | + (true, true) => return, |
| 108 | + (false, _) => ("pre", prefix), |
| 109 | + (true, false) => ("post", postfix), |
| 110 | + }; |
| 111 | + span_lint_and_help( |
| 112 | + cx, |
| 113 | + STRUCT_FIELD_NAMES, |
| 114 | + item_span, |
| 115 | + &format!("all fields have the same {what}fix: `{value}`"), |
| 116 | + None, |
| 117 | + &format!("remove the {what}fixes"), |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +fn check_struct_start(cx: &LateContext<'_>, item_name: &str, field: &FieldDef<'_>) { |
| 123 | + let item_name_snake = to_snake_case(item_name); |
| 124 | + let name = field.ident.name.as_str(); |
| 125 | + |
| 126 | + if name.starts_with(&item_name_snake) { |
| 127 | + span_lint_hir( |
| 128 | + cx, |
| 129 | + STRUCT_FIELD_NAMES, |
| 130 | + field.hir_id, |
| 131 | + field.span, |
| 132 | + "field name starts with the struct's name", |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +fn check_struct_end(cx: &LateContext<'_>, item_name: &str, field: &FieldDef<'_>) { |
| 138 | + let item_name_snake = to_snake_case(item_name); |
| 139 | + let name = field.ident.name.as_str(); |
| 140 | + |
| 141 | + if name.ends_with(&item_name_snake) { |
| 142 | + span_lint_hir( |
| 143 | + cx, |
| 144 | + STRUCT_FIELD_NAMES, |
| 145 | + field.hir_id, |
| 146 | + field.span, |
| 147 | + "field name ends with the struct's name", |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +impl LateLintPass<'_> for StructFields { |
| 153 | + fn check_item(&mut self, cx: &LateContext<'_>, item: &'_ rustc_hir::Item<'_>) { |
| 154 | + if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(item.owner_id.def_id) { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + let item_name = item.ident.name.as_str(); |
| 159 | + if let ItemKind::Struct(variant_data, _) = item.kind |
| 160 | + && let VariantData::Struct(fields, _) = variant_data { |
| 161 | + check_fields(cx, self.threshold, fields, item_name, item.span); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments