]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / utils / internal_lints / lint_without_lint_pass.rs
1 use crate::utils::internal_lints::metadata_collector::is_deprecated_lint;
2 use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
3 use clippy_utils::macros::root_macro_call_first_node;
4 use clippy_utils::{is_lint_allowed, match_def_path, paths};
5 use if_chain::if_chain;
6 use rustc_ast as ast;
7 use rustc_ast::ast::LitKind;
8 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
9 use rustc_hir as hir;
10 use rustc_hir::def::{DefKind, Res};
11 use rustc_hir::hir_id::CRATE_HIR_ID;
12 use rustc_hir::intravisit::Visitor;
13 use rustc_hir::{ExprKind, HirId, Item, MutTy, Mutability, Path, TyKind};
14 use rustc_lint::{LateContext, LateLintPass};
15 use rustc_middle::hir::nested_filter;
16 use rustc_semver::RustcVersion;
17 use rustc_session::{declare_tool_lint, impl_lint_pass};
18 use rustc_span::source_map::Spanned;
19 use rustc_span::symbol::Symbol;
20 use rustc_span::{sym, Span};
21
22 declare_clippy_lint! {
23 /// ### What it does
24 /// Ensures every lint is associated to a `LintPass`.
25 ///
26 /// ### Why is this bad?
27 /// The compiler only knows lints via a `LintPass`. Without
28 /// putting a lint to a `LintPass::get_lints()`'s return, the compiler will not
29 /// know the name of the lint.
30 ///
31 /// ### Known problems
32 /// Only checks for lints associated using the
33 /// `declare_lint_pass!`, `impl_lint_pass!`, and `lint_array!` macros.
34 ///
35 /// ### Example
36 /// ```rust,ignore
37 /// declare_lint! { pub LINT_1, ... }
38 /// declare_lint! { pub LINT_2, ... }
39 /// declare_lint! { pub FORGOTTEN_LINT, ... }
40 /// // ...
41 /// declare_lint_pass!(Pass => [LINT_1, LINT_2]);
42 /// // missing FORGOTTEN_LINT
43 /// ```
44 pub LINT_WITHOUT_LINT_PASS,
45 internal,
46 "declaring a lint without associating it in a LintPass"
47 }
48
49 declare_clippy_lint! {
50 /// ### What it does
51 /// Checks for cases of an auto-generated lint without an updated description,
52 /// i.e. `default lint description`.
53 ///
54 /// ### Why is this bad?
55 /// Indicates that the lint is not finished.
56 ///
57 /// ### Example
58 /// ```rust,ignore
59 /// declare_lint! { pub COOL_LINT, nursery, "default lint description" }
60 /// ```
61 ///
62 /// Use instead:
63 /// ```rust,ignore
64 /// declare_lint! { pub COOL_LINT, nursery, "a great new lint" }
65 /// ```
66 pub DEFAULT_LINT,
67 internal,
68 "found 'default lint description' in a lint declaration"
69 }
70
71 declare_clippy_lint! {
72 /// ### What it does
73 /// Checks for invalid `clippy::version` attributes.
74 ///
75 /// Valid values are:
76 /// * "pre 1.29.0"
77 /// * any valid semantic version
78 pub INVALID_CLIPPY_VERSION_ATTRIBUTE,
79 internal,
80 "found an invalid `clippy::version` attribute"
81 }
82
83 declare_clippy_lint! {
84 /// ### What it does
85 /// Checks for declared clippy lints without the `clippy::version` attribute.
86 ///
87 pub MISSING_CLIPPY_VERSION_ATTRIBUTE,
88 internal,
89 "found clippy lint without `clippy::version` attribute"
90 }
91
92 declare_clippy_lint! {
93 /// ### What it does
94 /// Checks for cases of an auto-generated deprecated lint without an updated reason,
95 /// i.e. `"default deprecation note"`.
96 ///
97 /// ### Why is this bad?
98 /// Indicates that the documentation is incomplete.
99 ///
100 /// ### Example
101 /// ```rust,ignore
102 /// declare_deprecated_lint! {
103 /// /// ### What it does
104 /// /// Nothing. This lint has been deprecated.
105 /// ///
106 /// /// ### Deprecation reason
107 /// /// TODO
108 /// #[clippy::version = "1.63.0"]
109 /// pub COOL_LINT,
110 /// "default deprecation note"
111 /// }
112 /// ```
113 ///
114 /// Use instead:
115 /// ```rust,ignore
116 /// declare_deprecated_lint! {
117 /// /// ### What it does
118 /// /// Nothing. This lint has been deprecated.
119 /// ///
120 /// /// ### Deprecation reason
121 /// /// This lint has been replaced by `cooler_lint`
122 /// #[clippy::version = "1.63.0"]
123 /// pub COOL_LINT,
124 /// "this lint has been replaced by `cooler_lint`"
125 /// }
126 /// ```
127 pub DEFAULT_DEPRECATION_REASON,
128 internal,
129 "found 'default deprecation note' in a deprecated lint declaration"
130 }
131
132 #[derive(Clone, Debug, Default)]
133 pub struct LintWithoutLintPass {
134 declared_lints: FxHashMap<Symbol, Span>,
135 registered_lints: FxHashSet<Symbol>,
136 }
137
138 impl_lint_pass!(LintWithoutLintPass => [DEFAULT_LINT, LINT_WITHOUT_LINT_PASS, INVALID_CLIPPY_VERSION_ATTRIBUTE, MISSING_CLIPPY_VERSION_ATTRIBUTE, DEFAULT_DEPRECATION_REASON]);
139
140 impl<'tcx> LateLintPass<'tcx> for LintWithoutLintPass {
141 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
142 if is_lint_allowed(cx, DEFAULT_LINT, item.hir_id())
143 || is_lint_allowed(cx, DEFAULT_DEPRECATION_REASON, item.hir_id())
144 {
145 return;
146 }
147
148 if let hir::ItemKind::Static(ty, Mutability::Not, body_id) = item.kind {
149 let is_lint_ref_ty = is_lint_ref_type(cx, ty);
150 if is_deprecated_lint(cx, ty) || is_lint_ref_ty {
151 check_invalid_clippy_version_attribute(cx, item);
152
153 let expr = &cx.tcx.hir().body(body_id).value;
154 let fields;
155 if is_lint_ref_ty {
156 if let ExprKind::AddrOf(_, _, inner_exp) = expr.kind
157 && let ExprKind::Struct(_, struct_fields, _) = inner_exp.kind {
158 fields = struct_fields;
159 } else {
160 return;
161 }
162 } else if let ExprKind::Struct(_, struct_fields, _) = expr.kind {
163 fields = struct_fields;
164 } else {
165 return;
166 }
167
168 let field = fields
169 .iter()
170 .find(|f| f.ident.as_str() == "desc")
171 .expect("lints must have a description field");
172
173 if let ExprKind::Lit(Spanned {
174 node: LitKind::Str(ref sym, _),
175 ..
176 }) = field.expr.kind
177 {
178 let sym_str = sym.as_str();
179 if is_lint_ref_ty {
180 if sym_str == "default lint description" {
181 span_lint(
182 cx,
183 DEFAULT_LINT,
184 item.span,
185 &format!("the lint `{}` has the default lint description", item.ident.name),
186 );
187 }
188
189 self.declared_lints.insert(item.ident.name, item.span);
190 } else if sym_str == "default deprecation note" {
191 span_lint(
192 cx,
193 DEFAULT_DEPRECATION_REASON,
194 item.span,
195 &format!("the lint `{}` has the default deprecation reason", item.ident.name),
196 );
197 }
198 }
199 }
200 } else if let Some(macro_call) = root_macro_call_first_node(cx, item) {
201 if !matches!(
202 cx.tcx.item_name(macro_call.def_id).as_str(),
203 "impl_lint_pass" | "declare_lint_pass"
204 ) {
205 return;
206 }
207 if let hir::ItemKind::Impl(hir::Impl {
208 of_trait: None,
209 items: impl_item_refs,
210 ..
211 }) = item.kind
212 {
213 let mut collector = LintCollector {
214 output: &mut self.registered_lints,
215 cx,
216 };
217 let body_id = cx.tcx.hir().body_owned_by(
218 cx.tcx.hir().local_def_id(
219 impl_item_refs
220 .iter()
221 .find(|iiref| iiref.ident.as_str() == "get_lints")
222 .expect("LintPass needs to implement get_lints")
223 .id
224 .hir_id(),
225 ),
226 );
227 collector.visit_expr(cx.tcx.hir().body(body_id).value);
228 }
229 }
230 }
231
232 fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
233 if is_lint_allowed(cx, LINT_WITHOUT_LINT_PASS, CRATE_HIR_ID) {
234 return;
235 }
236
237 for (lint_name, &lint_span) in &self.declared_lints {
238 // When using the `declare_tool_lint!` macro, the original `lint_span`'s
239 // file points to "<rustc macros>".
240 // `compiletest-rs` thinks that's an error in a different file and
241 // just ignores it. This causes the test in compile-fail/lint_pass
242 // not able to capture the error.
243 // Therefore, we need to climb the macro expansion tree and find the
244 // actual span that invoked `declare_tool_lint!`:
245 let lint_span = lint_span.ctxt().outer_expn_data().call_site;
246
247 if !self.registered_lints.contains(lint_name) {
248 span_lint(
249 cx,
250 LINT_WITHOUT_LINT_PASS,
251 lint_span,
252 &format!("the lint `{lint_name}` is not added to any `LintPass`"),
253 );
254 }
255 }
256 }
257 }
258
259 pub(super) fn is_lint_ref_type(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool {
260 if let TyKind::Ref(
261 _,
262 MutTy {
263 ty: inner,
264 mutbl: Mutability::Not,
265 },
266 ) = ty.kind
267 {
268 if let TyKind::Path(ref path) = inner.kind {
269 if let Res::Def(DefKind::Struct, def_id) = cx.qpath_res(path, inner.hir_id) {
270 return match_def_path(cx, def_id, &paths::LINT);
271 }
272 }
273 }
274
275 false
276 }
277
278 fn check_invalid_clippy_version_attribute(cx: &LateContext<'_>, item: &'_ Item<'_>) {
279 if let Some(value) = extract_clippy_version_value(cx, item) {
280 // The `sym!` macro doesn't work as it only expects a single token.
281 // It's better to keep it this way and have a direct `Symbol::intern` call here.
282 if value == Symbol::intern("pre 1.29.0") {
283 return;
284 }
285
286 if RustcVersion::parse(value.as_str()).is_err() {
287 span_lint_and_help(
288 cx,
289 INVALID_CLIPPY_VERSION_ATTRIBUTE,
290 item.span,
291 "this item has an invalid `clippy::version` attribute",
292 None,
293 "please use a valid semantic version, see `doc/adding_lints.md`",
294 );
295 }
296 } else {
297 span_lint_and_help(
298 cx,
299 MISSING_CLIPPY_VERSION_ATTRIBUTE,
300 item.span,
301 "this lint is missing the `clippy::version` attribute or version value",
302 None,
303 "please use a `clippy::version` attribute, see `doc/adding_lints.md`",
304 );
305 }
306 }
307
308 /// This function extracts the version value of a `clippy::version` attribute if the given value has
309 /// one
310 pub(super) fn extract_clippy_version_value(cx: &LateContext<'_>, item: &'_ Item<'_>) -> Option<Symbol> {
311 let attrs = cx.tcx.hir().attrs(item.hir_id());
312 attrs.iter().find_map(|attr| {
313 if_chain! {
314 // Identify attribute
315 if let ast::AttrKind::Normal(ref attr_kind) = &attr.kind;
316 if let [tool_name, attr_name] = &attr_kind.item.path.segments[..];
317 if tool_name.ident.name == sym::clippy;
318 if attr_name.ident.name == sym::version;
319 if let Some(version) = attr.value_str();
320 then { Some(version) } else { None }
321 }
322 })
323 }
324
325 struct LintCollector<'a, 'tcx> {
326 output: &'a mut FxHashSet<Symbol>,
327 cx: &'a LateContext<'tcx>,
328 }
329
330 impl<'a, 'tcx> Visitor<'tcx> for LintCollector<'a, 'tcx> {
331 type NestedFilter = nested_filter::All;
332
333 fn visit_path(&mut self, path: &Path<'_>, _: HirId) {
334 if path.segments.len() == 1 {
335 self.output.insert(path.segments[0].ident.name);
336 }
337 }
338
339 fn nested_visit_map(&mut self) -> Self::Map {
340 self.cx.tcx.hir()
341 }
342 }