]>
Commit | Line | Data |
---|---|---|
dfeec247 | 1 | use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; |
3dfed10e | 2 | use rustc_ast as ast; |
74b04a01 | 3 | use rustc_attr as attr; |
dfeec247 XL |
4 | use rustc_errors::Applicability; |
5 | use rustc_hir as hir; | |
6 | use rustc_hir::def::{DefKind, Res}; | |
7 | use rustc_hir::intravisit::FnKind; | |
8 | use rustc_hir::{GenericParamKind, PatKind}; | |
ba9703b0 | 9 | use rustc_middle::ty; |
dfeec247 XL |
10 | use rustc_span::symbol::sym; |
11 | use rustc_span::{symbol::Ident, BytePos, Span}; | |
83c7162d | 12 | use rustc_target::spec::abi::Abi; |
b039eaaf SL |
13 | |
14 | #[derive(PartialEq)] | |
15 | pub enum MethodLateContext { | |
abe05a73 | 16 | TraitAutoImpl, |
b039eaaf | 17 | TraitImpl, |
c30ab7b3 | 18 | PlainImpl, |
b039eaaf SL |
19 | } |
20 | ||
f035d41b | 21 | pub fn method_context(cx: &LateContext<'_>, id: hir::HirId) -> MethodLateContext { |
416331ca | 22 | let def_id = cx.tcx.hir().local_def_id(id); |
7cac9316 XL |
23 | let item = cx.tcx.associated_item(def_id); |
24 | match item.container { | |
abe05a73 | 25 | ty::TraitContainer(..) => MethodLateContext::TraitAutoImpl, |
dfeec247 XL |
26 | ty::ImplContainer(cid) => match cx.tcx.impl_trait_ref(cid) { |
27 | Some(_) => MethodLateContext::TraitImpl, | |
28 | None => MethodLateContext::PlainImpl, | |
29 | }, | |
b039eaaf SL |
30 | } |
31 | } | |
32 | ||
33 | declare_lint! { | |
1b1a35ee XL |
34 | /// The `non_camel_case_types` lint detects types, variants, traits and |
35 | /// type parameters that don't have camel case names. | |
36 | /// | |
37 | /// ### Example | |
38 | /// | |
39 | /// ```rust | |
40 | /// struct my_struct; | |
41 | /// ``` | |
42 | /// | |
43 | /// {{produces}} | |
44 | /// | |
45 | /// ### Explanation | |
46 | /// | |
47 | /// The preferred style for these identifiers is to use "camel case", such | |
48 | /// as `MyStruct`, where the first letter should not be lowercase, and | |
49 | /// should not use underscores between letters. Underscores are allowed at | |
50 | /// the beginning and end of the identifier, as well as between | |
51 | /// non-letters (such as `X86_64`). | |
b039eaaf SL |
52 | pub NON_CAMEL_CASE_TYPES, |
53 | Warn, | |
54 | "types, variants, traits and type parameters should have camel case names" | |
55 | } | |
56 | ||
532ac7d7 XL |
57 | declare_lint_pass!(NonCamelCaseTypes => [NON_CAMEL_CASE_TYPES]); |
58 | ||
5869c6ff XL |
59 | /// Some unicode characters *have* case, are considered upper case or lower case, but they *can't* |
60 | /// be upper cased or lower cased. For the purposes of the lint suggestion, we care about being able | |
61 | /// to change the char's case. | |
9fa01778 | 62 | fn char_has_case(c: char) -> bool { |
5869c6ff XL |
63 | let mut l = c.to_lowercase(); |
64 | let mut u = c.to_uppercase(); | |
65 | while let Some(l) = l.next() { | |
66 | match u.next() { | |
67 | Some(u) if l != u => return true, | |
68 | _ => {} | |
69 | } | |
70 | } | |
71 | u.next().is_some() | |
9fa01778 | 72 | } |
b039eaaf | 73 | |
9fa01778 XL |
74 | fn is_camel_case(name: &str) -> bool { |
75 | let name = name.trim_matches('_'); | |
76 | if name.is_empty() { | |
77 | return true; | |
78 | } | |
2c00a5a8 | 79 | |
9fa01778 XL |
80 | // start with a non-lowercase letter rather than non-uppercase |
81 | // ones (some scripts don't have a concept of upper/lowercase) | |
82 | !name.chars().next().unwrap().is_lowercase() | |
83 | && !name.contains("__") | |
1b1a35ee | 84 | && !name.chars().collect::<Vec<_>>().array_windows().any(|&[fst, snd]| { |
9fa01778 | 85 | // contains a capitalisable character followed by, or preceded by, an underscore |
1b1a35ee | 86 | char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_' |
9fa01778 XL |
87 | }) |
88 | } | |
89 | ||
90 | fn to_camel_case(s: &str) -> String { | |
91 | s.trim_matches('_') | |
92 | .split('_') | |
93 | .filter(|component| !component.is_empty()) | |
94 | .map(|component| { | |
95 | let mut camel_cased_component = String::new(); | |
96 | ||
97 | let mut new_word = true; | |
98 | let mut prev_is_lower_case = true; | |
99 | ||
100 | for c in component.chars() { | |
101 | // Preserve the case if an uppercase letter follows a lowercase letter, so that | |
102 | // `camelCase` is converted to `CamelCase`. | |
103 | if prev_is_lower_case && c.is_uppercase() { | |
104 | new_word = true; | |
105 | } | |
106 | ||
107 | if new_word { | |
fc512014 | 108 | camel_cased_component.extend(c.to_uppercase()); |
9fa01778 | 109 | } else { |
fc512014 | 110 | camel_cased_component.extend(c.to_lowercase()); |
9fa01778 XL |
111 | } |
112 | ||
113 | prev_is_lower_case = c.is_lowercase(); | |
114 | new_word = false; | |
b039eaaf | 115 | } |
b039eaaf | 116 | |
9fa01778 XL |
117 | camel_cased_component |
118 | }) | |
dfeec247 XL |
119 | .fold((String::new(), None), |(acc, prev): (String, Option<String>), next| { |
120 | // separate two components with an underscore if their boundary cannot | |
94222f64 | 121 | // be distinguished using an uppercase/lowercase case distinction |
dfeec247 XL |
122 | let join = if let Some(prev) = prev { |
123 | let l = prev.chars().last().unwrap(); | |
124 | let f = next.chars().next().unwrap(); | |
125 | !char_has_case(l) && !char_has_case(f) | |
126 | } else { | |
127 | false | |
128 | }; | |
129 | (acc + if join { "_" } else { "" } + &next, Some(next)) | |
130 | }) | |
9fa01778 XL |
131 | .0 |
132 | } | |
b039eaaf | 133 | |
9fa01778 XL |
134 | impl NonCamelCaseTypes { |
135 | fn check_case(&self, cx: &EarlyContext<'_>, sort: &str, ident: &Ident) { | |
0731742a XL |
136 | let name = &ident.name.as_str(); |
137 | ||
b039eaaf | 138 | if !is_camel_case(name) { |
74b04a01 XL |
139 | cx.struct_span_lint(NON_CAMEL_CASE_TYPES, ident.span, |lint| { |
140 | let msg = format!("{} `{}` should have an upper camel case name", sort, name); | |
fc512014 XL |
141 | let mut err = lint.build(&msg); |
142 | let cc = to_camel_case(name); | |
143 | // We cannot provide meaningful suggestions | |
144 | // if the characters are in the category of "Lowercase Letter". | |
145 | if *name != cc { | |
146 | err.span_suggestion( | |
74b04a01 XL |
147 | ident.span, |
148 | "convert the identifier to upper camel case", | |
149 | to_camel_case(name), | |
150 | Applicability::MaybeIncorrect, | |
fc512014 | 151 | ); |
5869c6ff XL |
152 | } else { |
153 | err.span_label(ident.span, "should have an UpperCamelCase name"); | |
fc512014 XL |
154 | } |
155 | ||
156 | err.emit(); | |
74b04a01 | 157 | }) |
b039eaaf SL |
158 | } |
159 | } | |
160 | } | |
161 | ||
0731742a | 162 | impl EarlyLintPass for NonCamelCaseTypes { |
9fa01778 | 163 | fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) { |
dfeec247 XL |
164 | let has_repr_c = it |
165 | .attrs | |
c30ab7b3 | 166 | .iter() |
3dfed10e | 167 | .any(|attr| attr::find_repr_attrs(&cx.sess, attr).contains(&attr::ReprC)); |
b039eaaf | 168 | |
2c00a5a8 | 169 | if has_repr_c { |
b039eaaf SL |
170 | return; |
171 | } | |
172 | ||
e74abb32 | 173 | match it.kind { |
dfeec247 XL |
174 | ast::ItemKind::TyAlias(..) |
175 | | ast::ItemKind::Enum(..) | |
176 | | ast::ItemKind::Struct(..) | |
177 | | ast::ItemKind::Union(..) => self.check_case(cx, "type", &it.ident), | |
0731742a | 178 | ast::ItemKind::Trait(..) => self.check_case(cx, "trait", &it.ident), |
17df50a5 | 179 | ast::ItemKind::TraitAlias(..) => self.check_case(cx, "trait alias", &it.ident), |
c30ab7b3 | 180 | _ => (), |
b039eaaf SL |
181 | } |
182 | } | |
183 | ||
dfeec247 XL |
184 | fn check_trait_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) { |
185 | if let ast::AssocItemKind::TyAlias(..) = it.kind { | |
186 | self.check_case(cx, "associated type", &it.ident); | |
187 | } | |
188 | } | |
189 | ||
e1599b0c XL |
190 | fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) { |
191 | self.check_case(cx, "variant", &v.ident); | |
8bb4bdeb XL |
192 | } |
193 | ||
9fa01778 | 194 | fn check_generic_param(&mut self, cx: &EarlyContext<'_>, param: &ast::GenericParam) { |
0731742a XL |
195 | if let ast::GenericParamKind::Type { .. } = param.kind { |
196 | self.check_case(cx, "type parameter", ¶m.ident); | |
b039eaaf SL |
197 | } |
198 | } | |
199 | } | |
200 | ||
201 | declare_lint! { | |
1b1a35ee XL |
202 | /// The `non_snake_case` lint detects variables, methods, functions, |
203 | /// lifetime parameters and modules that don't have snake case names. | |
204 | /// | |
205 | /// ### Example | |
206 | /// | |
207 | /// ```rust | |
208 | /// let MY_VALUE = 5; | |
209 | /// ``` | |
210 | /// | |
211 | /// {{produces}} | |
212 | /// | |
213 | /// ### Explanation | |
214 | /// | |
215 | /// The preferred style for these identifiers is to use "snake case", | |
216 | /// where all the characters are in lowercase, with words separated with a | |
217 | /// single underscore, such as `my_value`. | |
b039eaaf SL |
218 | pub NON_SNAKE_CASE, |
219 | Warn, | |
92a42be0 | 220 | "variables, methods, functions, lifetime parameters and modules should have snake case names" |
b039eaaf SL |
221 | } |
222 | ||
532ac7d7 | 223 | declare_lint_pass!(NonSnakeCase => [NON_SNAKE_CASE]); |
b039eaaf SL |
224 | |
225 | impl NonSnakeCase { | |
226 | fn to_snake_case(mut str: &str) -> String { | |
227 | let mut words = vec![]; | |
228 | // Preserve leading underscores | |
0731742a | 229 | str = str.trim_start_matches(|c: char| { |
b039eaaf SL |
230 | if c == '_' { |
231 | words.push(String::new()); | |
232 | true | |
233 | } else { | |
234 | false | |
235 | } | |
236 | }); | |
237 | for s in str.split('_') { | |
238 | let mut last_upper = false; | |
239 | let mut buf = String::new(); | |
240 | if s.is_empty() { | |
241 | continue; | |
242 | } | |
243 | for ch in s.chars() { | |
c30ab7b3 | 244 | if !buf.is_empty() && buf != "'" && ch.is_uppercase() && !last_upper { |
b039eaaf SL |
245 | words.push(buf); |
246 | buf = String::new(); | |
247 | } | |
248 | last_upper = ch.is_uppercase(); | |
249 | buf.extend(ch.to_lowercase()); | |
250 | } | |
251 | words.push(buf); | |
252 | } | |
253 | words.join("_") | |
254 | } | |
255 | ||
0731742a | 256 | /// Checks if a given identifier is snake case, and reports a diagnostic if not. |
f035d41b | 257 | fn check_snake_case(&self, cx: &LateContext<'_>, sort: &str, ident: &Ident) { |
b039eaaf SL |
258 | fn is_snake_case(ident: &str) -> bool { |
259 | if ident.is_empty() { | |
260 | return true; | |
261 | } | |
0731742a | 262 | let ident = ident.trim_start_matches('\''); |
b039eaaf SL |
263 | let ident = ident.trim_matches('_'); |
264 | ||
265 | let mut allow_underscore = true; | |
266 | ident.chars().all(|c| { | |
267 | allow_underscore = match c { | |
268 | '_' if !allow_underscore => return false, | |
269 | '_' => false, | |
270 | // It would be more obvious to use `c.is_lowercase()`, | |
271 | // but some characters do not have a lowercase form | |
272 | c if !c.is_uppercase() => true, | |
273 | _ => return false, | |
274 | }; | |
275 | true | |
276 | }) | |
277 | } | |
278 | ||
0731742a XL |
279 | let name = &ident.name.as_str(); |
280 | ||
b039eaaf | 281 | if !is_snake_case(name) { |
74b04a01 XL |
282 | cx.struct_span_lint(NON_SNAKE_CASE, ident.span, |lint| { |
283 | let sc = NonSnakeCase::to_snake_case(name); | |
284 | let msg = format!("{} `{}` should have a snake case name", sort, name); | |
285 | let mut err = lint.build(&msg); | |
fc512014 XL |
286 | // We cannot provide meaningful suggestions |
287 | // if the characters are in the category of "Uppercase Letter". | |
288 | if *name != sc { | |
289 | // We have a valid span in almost all cases, but we don't have one when linting a crate | |
290 | // name provided via the command line. | |
291 | if !ident.span.is_dummy() { | |
5869c6ff XL |
292 | let sc_ident = Ident::from_str_and_span(&sc, ident.span); |
293 | let (message, suggestion) = if sc_ident.is_reserved() { | |
294 | // We shouldn't suggest a reserved identifier to fix non-snake-case identifiers. | |
295 | // Instead, recommend renaming the identifier entirely or, if permitted, | |
296 | // escaping it to create a raw identifier. | |
297 | if sc_ident.name.can_be_raw() { | |
298 | ("rename the identifier or convert it to a snake case raw identifier", sc_ident.to_string()) | |
299 | } else { | |
300 | err.note(&format!("`{}` cannot be used as a raw identifier", sc)); | |
301 | ("rename the identifier", String::new()) | |
302 | } | |
303 | } else { | |
304 | ("convert the identifier to snake case", sc) | |
305 | }; | |
306 | ||
fc512014 XL |
307 | err.span_suggestion( |
308 | ident.span, | |
5869c6ff XL |
309 | message, |
310 | suggestion, | |
fc512014 XL |
311 | Applicability::MaybeIncorrect, |
312 | ); | |
313 | } else { | |
314 | err.help(&format!("convert the identifier to snake case: `{}`", sc)); | |
315 | } | |
5869c6ff XL |
316 | } else { |
317 | err.span_label(ident.span, "should have a snake_case name"); | |
74b04a01 | 318 | } |
0731742a | 319 | |
74b04a01 XL |
320 | err.emit(); |
321 | }); | |
b039eaaf SL |
322 | } |
323 | } | |
324 | } | |
325 | ||
f035d41b | 326 | impl<'tcx> LateLintPass<'tcx> for NonSnakeCase { |
dfeec247 XL |
327 | fn check_mod( |
328 | &mut self, | |
f035d41b | 329 | cx: &LateContext<'_>, |
dfeec247 XL |
330 | _: &'tcx hir::Mod<'tcx>, |
331 | _: Span, | |
332 | id: hir::HirId, | |
333 | ) { | |
532ac7d7 XL |
334 | if id != hir::CRATE_HIR_ID { |
335 | return; | |
336 | } | |
337 | ||
0731742a XL |
338 | let crate_ident = if let Some(name) = &cx.tcx.sess.opts.crate_name { |
339 | Some(Ident::from_str(name)) | |
340 | } else { | |
3dfed10e XL |
341 | cx.sess() |
342 | .find_by_name(&cx.tcx.hir().attrs(hir::CRATE_HIR_ID), sym::crate_name) | |
0731742a XL |
343 | .and_then(|attr| attr.meta()) |
344 | .and_then(|meta| { | |
345 | meta.name_value_literal().and_then(|lit| { | |
e74abb32 | 346 | if let ast::LitKind::Str(name, ..) = lit.kind { |
0731742a | 347 | // Discard the double quotes surrounding the literal. |
dfeec247 XL |
348 | let sp = cx |
349 | .sess() | |
350 | .source_map() | |
351 | .span_to_snippet(lit.span) | |
0731742a XL |
352 | .ok() |
353 | .and_then(|snippet| { | |
354 | let left = snippet.find('"')?; | |
dfeec247 XL |
355 | let right = |
356 | snippet.rfind('"').map(|pos| snippet.len() - pos)?; | |
0731742a XL |
357 | |
358 | Some( | |
359 | lit.span | |
360 | .with_lo(lit.span.lo() + BytePos(left as u32 + 1)) | |
361 | .with_hi(lit.span.hi() - BytePos(right as u32)), | |
362 | ) | |
363 | }) | |
29967ef6 | 364 | .unwrap_or(lit.span); |
0731742a XL |
365 | |
366 | Some(Ident::new(name, sp)) | |
367 | } else { | |
368 | None | |
369 | } | |
370 | }) | |
371 | }) | |
372 | }; | |
373 | ||
374 | if let Some(ident) = &crate_ident { | |
375 | self.check_snake_case(cx, "crate", ident); | |
b039eaaf SL |
376 | } |
377 | } | |
378 | ||
f035d41b | 379 | fn check_generic_param(&mut self, cx: &LateContext<'_>, param: &hir::GenericParam<'_>) { |
0731742a XL |
380 | if let GenericParamKind::Lifetime { .. } = param.kind { |
381 | self.check_snake_case(cx, "lifetime", ¶m.name.ident()); | |
ff7c6d11 XL |
382 | } |
383 | } | |
384 | ||
0731742a XL |
385 | fn check_fn( |
386 | &mut self, | |
f035d41b | 387 | cx: &LateContext<'_>, |
9fa01778 | 388 | fk: FnKind<'_>, |
dfeec247 XL |
389 | _: &hir::FnDecl<'_>, |
390 | _: &hir::Body<'_>, | |
0731742a | 391 | _: Span, |
9fa01778 | 392 | id: hir::HirId, |
0731742a | 393 | ) { |
94222f64 | 394 | let attrs = cx.tcx.hir().attrs(id); |
0731742a | 395 | match &fk { |
94222f64 | 396 | FnKind::Method(ident, sig, ..) => match method_context(cx, id) { |
dfeec247 | 397 | MethodLateContext::PlainImpl => { |
94222f64 XL |
398 | if sig.header.abi != Abi::Rust && cx.sess().contains_name(attrs, sym::no_mangle) |
399 | { | |
400 | return; | |
401 | } | |
dfeec247 | 402 | self.check_snake_case(cx, "method", ident); |
c30ab7b3 | 403 | } |
dfeec247 XL |
404 | MethodLateContext::TraitAutoImpl => { |
405 | self.check_snake_case(cx, "trait method", ident); | |
406 | } | |
407 | _ => (), | |
408 | }, | |
6a06907d | 409 | FnKind::ItemFn(ident, _, header, _) => { |
ea8adc8c | 410 | // Skip foreign-ABI #[no_mangle] functions (Issue #31924) |
3dfed10e | 411 | if header.abi != Abi::Rust && cx.sess().contains_name(attrs, sym::no_mangle) { |
ea8adc8c XL |
412 | return; |
413 | } | |
0731742a | 414 | self.check_snake_case(cx, "function", ident); |
c30ab7b3 | 415 | } |
6a06907d | 416 | FnKind::Closure => (), |
b039eaaf SL |
417 | } |
418 | } | |
419 | ||
f035d41b | 420 | fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) { |
e74abb32 | 421 | if let hir::ItemKind::Mod(_) = it.kind { |
0731742a | 422 | self.check_snake_case(cx, "module", &it.ident); |
b039eaaf SL |
423 | } |
424 | } | |
425 | ||
f035d41b | 426 | fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &hir::TraitItem<'_>) { |
ba9703b0 | 427 | if let hir::TraitItemKind::Fn(_, hir::TraitFn::Required(pnames)) = item.kind { |
0731742a | 428 | self.check_snake_case(cx, "trait method", &item.ident); |
8faf50e0 | 429 | for param_name in pnames { |
0731742a | 430 | self.check_snake_case(cx, "variable", param_name); |
32a655c1 | 431 | } |
b039eaaf SL |
432 | } |
433 | } | |
434 | ||
f035d41b | 435 | fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) { |
5869c6ff | 436 | if let PatKind::Binding(_, hid, ident, _) = p.kind { |
dfeec247 XL |
437 | if let hir::Node::Pat(parent_pat) = cx.tcx.hir().get(cx.tcx.hir().get_parent_node(hid)) |
438 | { | |
439 | if let PatKind::Struct(_, field_pats, _) = &parent_pat.kind { | |
c295e0f8 XL |
440 | if field_pats |
441 | .iter() | |
442 | .any(|field| !field.is_shorthand && field.pat.hir_id == p.hir_id) | |
443 | { | |
444 | // Only check if a new name has been introduced, to avoid warning | |
445 | // on both the struct definition and this pattern. | |
446 | self.check_snake_case(cx, "variable", &ident); | |
dfeec247 XL |
447 | } |
448 | return; | |
449 | } | |
450 | } | |
0731742a | 451 | self.check_snake_case(cx, "variable", &ident); |
476ff2be | 452 | } |
b039eaaf SL |
453 | } |
454 | ||
f035d41b | 455 | fn check_struct_def(&mut self, cx: &LateContext<'_>, s: &hir::VariantData<'_>) { |
b039eaaf | 456 | for sf in s.fields() { |
0731742a | 457 | self.check_snake_case(cx, "structure field", &sf.ident); |
b039eaaf SL |
458 | } |
459 | } | |
460 | } | |
461 | ||
462 | declare_lint! { | |
1b1a35ee XL |
463 | /// The `non_upper_case_globals` lint detects static items that don't have |
464 | /// uppercase identifiers. | |
465 | /// | |
466 | /// ### Example | |
467 | /// | |
468 | /// ```rust | |
469 | /// static max_points: i32 = 5; | |
470 | /// ``` | |
471 | /// | |
472 | /// {{produces}} | |
473 | /// | |
474 | /// ### Explanation | |
475 | /// | |
476 | /// The preferred style is for static item names to use all uppercase | |
477 | /// letters such as `MAX_POINTS`. | |
b039eaaf SL |
478 | pub NON_UPPER_CASE_GLOBALS, |
479 | Warn, | |
480 | "static constants should have uppercase identifiers" | |
481 | } | |
482 | ||
532ac7d7 | 483 | declare_lint_pass!(NonUpperCaseGlobals => [NON_UPPER_CASE_GLOBALS]); |
b039eaaf SL |
484 | |
485 | impl NonUpperCaseGlobals { | |
f035d41b | 486 | fn check_upper_case(cx: &LateContext<'_>, sort: &str, ident: &Ident) { |
0731742a | 487 | let name = &ident.name.as_str(); |
0731742a | 488 | if name.chars().any(|c| c.is_lowercase()) { |
74b04a01 XL |
489 | cx.struct_span_lint(NON_UPPER_CASE_GLOBALS, ident.span, |lint| { |
490 | let uc = NonSnakeCase::to_snake_case(&name).to_uppercase(); | |
fc512014 XL |
491 | let mut err = |
492 | lint.build(&format!("{} `{}` should have an upper case name", sort, name)); | |
493 | // We cannot provide meaningful suggestions | |
494 | // if the characters are in the category of "Lowercase Letter". | |
495 | if *name != uc { | |
496 | err.span_suggestion( | |
74b04a01 XL |
497 | ident.span, |
498 | "convert the identifier to upper case", | |
499 | uc, | |
500 | Applicability::MaybeIncorrect, | |
fc512014 | 501 | ); |
5869c6ff XL |
502 | } else { |
503 | err.span_label(ident.span, "should have an UPPER_CASE name"); | |
fc512014 XL |
504 | } |
505 | ||
506 | err.emit(); | |
74b04a01 | 507 | }) |
b039eaaf SL |
508 | } |
509 | } | |
510 | } | |
511 | ||
f035d41b XL |
512 | impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals { |
513 | fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) { | |
6a06907d | 514 | let attrs = cx.tcx.hir().attrs(it.hir_id()); |
e74abb32 | 515 | match it.kind { |
6a06907d | 516 | hir::ItemKind::Static(..) if !cx.sess().contains_name(attrs, sym::no_mangle) => { |
0731742a | 517 | NonUpperCaseGlobals::check_upper_case(cx, "static variable", &it.ident); |
b039eaaf | 518 | } |
8faf50e0 | 519 | hir::ItemKind::Const(..) => { |
0731742a | 520 | NonUpperCaseGlobals::check_upper_case(cx, "constant", &it.ident); |
b039eaaf SL |
521 | } |
522 | _ => {} | |
523 | } | |
524 | } | |
525 | ||
f035d41b | 526 | fn check_trait_item(&mut self, cx: &LateContext<'_>, ti: &hir::TraitItem<'_>) { |
e74abb32 | 527 | if let hir::TraitItemKind::Const(..) = ti.kind { |
0731742a | 528 | NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &ti.ident); |
b039eaaf SL |
529 | } |
530 | } | |
531 | ||
f035d41b | 532 | fn check_impl_item(&mut self, cx: &LateContext<'_>, ii: &hir::ImplItem<'_>) { |
e74abb32 | 533 | if let hir::ImplItemKind::Const(..) = ii.kind { |
0731742a | 534 | NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &ii.ident); |
b039eaaf SL |
535 | } |
536 | } | |
537 | ||
f035d41b | 538 | fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) { |
b039eaaf | 539 | // Lint for constants that look like binding identifiers (#7526) |
e74abb32 | 540 | if let PatKind::Path(hir::QPath::Resolved(None, ref path)) = p.kind { |
48663c56 | 541 | if let Res::Def(DefKind::Const, _) = path.res { |
32a655c1 | 542 | if path.segments.len() == 1 { |
0731742a XL |
543 | NonUpperCaseGlobals::check_upper_case( |
544 | cx, | |
545 | "constant in pattern", | |
dfeec247 | 546 | &path.segments[0].ident, |
0731742a | 547 | ); |
3157f602 | 548 | } |
b039eaaf | 549 | } |
b039eaaf SL |
550 | } |
551 | } | |
9fa01778 | 552 | |
f035d41b | 553 | fn check_generic_param(&mut self, cx: &LateContext<'_>, param: &hir::GenericParam<'_>) { |
9fa01778 | 554 | if let GenericParamKind::Const { .. } = param.kind { |
dfeec247 | 555 | NonUpperCaseGlobals::check_upper_case(cx, "const parameter", ¶m.name.ident()); |
9fa01778 XL |
556 | } |
557 | } | |
558 | } | |
559 | ||
560 | #[cfg(test)] | |
dc9dc135 | 561 | mod tests; |