Skip to content

Commit badd57c

Browse files
committed
give functions more descriptive names, add docs
1 parent a5c08fd commit badd57c

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

clippy_lints/src/matches/significant_drop_in_scrutinee.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::FxHashSet;
44
use clippy_utils::diagnostics::span_lint_and_then;
55
use clippy_utils::source::{first_line_of_span, indent_of, snippet};
66
use clippy_utils::ty::{for_each_top_level_late_bound_region, is_copy};
7-
use clippy_utils::{get_attr, is_lint_allowed, sym};
7+
use clippy_utils::{get_builtin_attr, is_lint_allowed, sym};
88
use itertools::Itertools;
99
use rustc_ast::Mutability;
1010
use rustc_data_structures::fx::FxIndexSet;
@@ -183,7 +183,7 @@ impl<'a, 'tcx> SigDropChecker<'a, 'tcx> {
183183

184184
fn has_sig_drop_attr_impl(&mut self, ty: Ty<'tcx>) -> bool {
185185
if let Some(adt) = ty.ty_adt_def()
186-
&& get_attr(
186+
&& get_builtin_attr(
187187
self.cx.sess(),
188188
self.cx.tcx.get_all_attrs(adt.did()),
189189
sym::has_significant_drop,

clippy_lints/src/significant_drop_tightening.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::{indent_of, snippet};
3-
use clippy_utils::{expr_or_init, get_attr, path_to_local, peel_hir_expr_unary, sym};
3+
use clippy_utils::{expr_or_init, get_builtin_attr, path_to_local, peel_hir_expr_unary, sym};
44
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
55
use rustc_errors::Applicability;
66
use rustc_hir::def::{DefKind, Res};
@@ -166,7 +166,7 @@ impl<'cx, 'others, 'tcx> AttrChecker<'cx, 'others, 'tcx> {
166166

167167
fn has_sig_drop_attr_uncached(&mut self, ty: Ty<'tcx>, depth: usize) -> bool {
168168
if let Some(adt) = ty.ty_adt_def() {
169-
let mut iter = get_attr(
169+
let mut iter = get_builtin_attr(
170170
self.cx.sess(),
171171
self.cx.tcx.get_all_attrs(adt.did()),
172172
sym::has_significant_drop,

clippy_lints/src/utils/author.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::{MaybePath, get_attr, higher, path_def_id, sym};
1+
use clippy_utils::{MaybePath, get_builtin_attr, higher, path_def_id, sym};
22
use itertools::Itertools;
33
use rustc_ast::LitIntType;
44
use rustc_ast::ast::{LitFloatType, LitKind};
@@ -856,5 +856,5 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
856856

857857
fn has_attr(cx: &LateContext<'_>, hir_id: HirId) -> bool {
858858
let attrs = cx.tcx.hir_attrs(hir_id);
859-
get_attr(cx.sess(), attrs, sym::author).count() > 0
859+
get_builtin_attr(cx.sess(), attrs, sym::author).count() > 0
860860
}

clippy_lints/src/utils/dump_hir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::{get_attr, sym};
1+
use clippy_utils::{get_builtin_attr, sym};
22
use hir::TraitItem;
33
use rustc_hir as hir;
44
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -60,5 +60,5 @@ impl<'tcx> LateLintPass<'tcx> for DumpHir {
6060

6161
fn has_attr(cx: &LateContext<'_>, hir_id: hir::HirId) -> bool {
6262
let attrs = cx.tcx.hir_attrs(hir_id);
63-
get_attr(cx.sess(), attrs, sym::dump).count() > 0
63+
get_builtin_attr(cx.sess(), attrs, sym::dump).count() > 0
6464
}

clippy_utils/src/attrs.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Utility functions for attributes, including Clippy's built-in ones
2+
13
use crate::source::SpanRangeExt;
24
use crate::{sym, tokenize_with_text};
35
use rustc_ast::attr;
@@ -16,10 +18,12 @@ use std::str::FromStr;
1618
pub enum DeprecationStatus {
1719
/// Attribute is deprecated and was possibly replaced by the named attribute
1820
Deprecated(Option<&'static str>),
21+
/// Attribute is currently used
1922
None,
2023
}
2124

2225
#[rustfmt::skip]
26+
/// Attributes known by Clippy
2327
pub const BUILTIN_ATTRIBUTES: &[(Symbol, DeprecationStatus)] = &[
2428
(sym::author, DeprecationStatus::None),
2529
(sym::version, DeprecationStatus::None),
@@ -43,6 +47,7 @@ impl Drop for LimitStack {
4347
}
4448
}
4549

50+
#[expect(missing_docs, reason = "they're all trivial...")]
4651
impl LimitStack {
4752
#[must_use]
4853
pub fn new(limit: u64) -> Self {
@@ -61,7 +66,8 @@ impl LimitStack {
6166
}
6267
}
6368

64-
pub fn get_attr<'a, A: AttributeExt + 'a>(
69+
/// Given `attrs`, extract all the instances of a built-in Clippy attribute called `name`
70+
pub fn get_builtin_attr<'a, A: AttributeExt + 'a>(
6571
sess: &'a Session,
6672
attrs: &'a [A],
6773
name: Symbol,
@@ -104,7 +110,7 @@ pub fn get_attr<'a, A: AttributeExt + 'a>(
104110
}
105111

106112
fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[impl AttributeExt], name: Symbol, mut f: F) {
107-
for attr in get_attr(sess, attrs, name) {
113+
for attr in get_builtin_attr(sess, attrs, name) {
108114
let Some(value) = attr.value_str() else {
109115
sess.dcx().span_err(attr.span(), "bad clippy attribute");
110116
continue;
@@ -117,9 +123,11 @@ fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[impl AttributeExt], name:
117123
}
118124
}
119125

120-
pub fn get_unique_attr<'a, A: AttributeExt>(sess: &'a Session, attrs: &'a [A], name: Symbol) -> Option<&'a A> {
126+
/// If `attrs` contain exactly one instance of a built-in Clippy attribute called `name`,
127+
/// returns that attribute, and `None` otherwise
128+
pub fn get_unique_builtin_attr<'a, A: AttributeExt>(sess: &'a Session, attrs: &'a [A], name: Symbol) -> Option<&'a A> {
121129
let mut unique_attr: Option<&A> = None;
122-
for attr in get_attr(sess, attrs, name) {
130+
for attr in get_builtin_attr(sess, attrs, name) {
123131
if let Some(duplicate) = unique_attr {
124132
sess.dcx()
125133
.struct_span_err(attr.span(), format!("`{name}` is defined multiple times"))
@@ -132,13 +140,13 @@ pub fn get_unique_attr<'a, A: AttributeExt>(sess: &'a Session, attrs: &'a [A], n
132140
unique_attr
133141
}
134142

135-
/// Returns true if the attributes contain any of `proc_macro`,
136-
/// `proc_macro_derive` or `proc_macro_attribute`, false otherwise
143+
/// Checks whether `attrs` contain any of `proc_macro`, `proc_macro_derive` or
144+
/// `proc_macro_attribute`
137145
pub fn is_proc_macro(attrs: &[impl AttributeExt]) -> bool {
138146
attrs.iter().any(AttributeExt::is_proc_macro_attr)
139147
}
140148

141-
/// Returns true if the attributes contain `#[doc(hidden)]`
149+
/// Checks whether `attrs` contain `#[doc(hidden)]`
142150
pub fn is_doc_hidden(attrs: &[impl AttributeExt]) -> bool {
143151
attrs
144152
.iter()
@@ -147,6 +155,7 @@ pub fn is_doc_hidden(attrs: &[impl AttributeExt]) -> bool {
147155
.any(|l| attr::list_contains_name(&l, sym::hidden))
148156
}
149157

158+
/// Checks whether the given ADT, or any of its fields/variants, are marked as `#[non_exhaustive]`
150159
pub fn has_non_exhaustive_attr(tcx: TyCtxt<'_>, adt: AdtDef<'_>) -> bool {
151160
adt.is_variant_list_non_exhaustive()
152161
|| find_attr!(tcx.get_all_attrs(adt.did()), AttributeKind::NonExhaustive(..))
@@ -159,7 +168,7 @@ pub fn has_non_exhaustive_attr(tcx: TyCtxt<'_>, adt: AdtDef<'_>) -> bool {
159168
.any(|field_def| find_attr!(tcx.get_all_attrs(field_def.did), AttributeKind::NonExhaustive(..)))
160169
}
161170

162-
/// Checks if the given span contains a `#[cfg(..)]` attribute
171+
/// Checks whether the given span contains a `#[cfg(..)]` attribute
163172
pub fn span_contains_cfg(cx: &LateContext<'_>, s: Span) -> bool {
164173
s.check_source_text(cx, |src| {
165174
let mut iter = tokenize_with_text(src);

clippy_utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern crate rustc_trait_selection;
5050
extern crate smallvec;
5151

5252
pub mod ast_utils;
53+
#[deny(missing_docs)]
5354
pub mod attrs;
5455
mod check_proc_macro;
5556
pub mod comparisons;

clippy_utils/src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::sync::{Arc, OnceLock};
44

55
use crate::visitors::{Descend, for_each_expr_without_closures};
6-
use crate::{get_unique_attr, sym};
6+
use crate::{get_unique_builtin_attr, sym};
77

88
use arrayvec::ArrayVec;
99
use rustc_ast::{FormatArgs, FormatArgument, FormatPlaceholder};
@@ -42,7 +42,7 @@ pub fn is_format_macro(cx: &LateContext<'_>, macro_def_id: DefId) -> bool {
4242
} else {
4343
// Allow users to tag any macro as being format!-like
4444
// TODO: consider deleting FORMAT_MACRO_DIAG_ITEMS and using just this method
45-
get_unique_attr(cx.sess(), cx.tcx.get_all_attrs(macro_def_id), sym::format_args).is_some()
45+
get_unique_builtin_attr(cx.sess(), cx.tcx.get_all_attrs(macro_def_id), sym::format_args).is_some()
4646
}
4747
}
4848

0 commit comments

Comments
 (0)