Skip to content

Commit 39ddb78

Browse files
committed
give functions more descriptive names, add docs
1 parent 4beaaa0 commit 39ddb78

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;
@@ -12,7 +14,8 @@ use rustc_session::Session;
1214
use rustc_span::{Span, Symbol};
1315
use std::str::FromStr;
1416

15-
pub fn get_attr<'a, A: AttributeExt + 'a>(
17+
/// Given `attrs`, extract all the instances of a built-in Clippy attribute called `name`
18+
pub fn get_builtin_attr<'a, A: AttributeExt + 'a>(
1619
sess: &'a Session,
1720
attrs: &'a [A],
1821
name: Symbol,
@@ -59,9 +62,11 @@ pub fn get_attr<'a, A: AttributeExt + 'a>(
5962
})
6063
}
6164

62-
pub fn get_unique_attr<'a, A: AttributeExt>(sess: &'a Session, attrs: &'a [A], name: Symbol) -> Option<&'a A> {
65+
/// If `attrs` contain exactly one instance of a built-in Clippy attribute called `name`,
66+
/// returns that attribute, and `None` otherwise
67+
pub fn get_unique_builtin_attr<'a, A: AttributeExt>(sess: &'a Session, attrs: &'a [A], name: Symbol) -> Option<&'a A> {
6368
let mut unique_attr: Option<&A> = None;
64-
for attr in get_attr(sess, attrs, name) {
69+
for attr in get_builtin_attr(sess, attrs, name) {
6570
if let Some(duplicate) = unique_attr {
6671
sess.dcx()
6772
.struct_span_err(attr.span(), format!("`{name}` is defined multiple times"))
@@ -74,13 +79,13 @@ pub fn get_unique_attr<'a, A: AttributeExt>(sess: &'a Session, attrs: &'a [A], n
7479
unique_attr
7580
}
7681

77-
/// Returns true if the attributes contain any of `proc_macro`,
78-
/// `proc_macro_derive` or `proc_macro_attribute`, false otherwise
82+
/// Checks whether `attrs` contain any of `proc_macro`, `proc_macro_derive` or
83+
/// `proc_macro_attribute`
7984
pub fn is_proc_macro(attrs: &[impl AttributeExt]) -> bool {
8085
attrs.iter().any(AttributeExt::is_proc_macro_attr)
8186
}
8287

83-
/// Returns true if the attributes contain `#[doc(hidden)]`
88+
/// Checks whether `attrs` contain `#[doc(hidden)]`
8489
pub fn is_doc_hidden(attrs: &[impl AttributeExt]) -> bool {
8590
attrs
8691
.iter()
@@ -89,6 +94,7 @@ pub fn is_doc_hidden(attrs: &[impl AttributeExt]) -> bool {
8994
.any(|l| attr::list_contains_name(&l, sym::hidden))
9095
}
9196

97+
/// Checks whether the given ADT, or any of its fields/variants, are marked as `#[non_exhaustive]`
9298
pub fn has_non_exhaustive_attr(tcx: TyCtxt<'_>, adt: AdtDef<'_>) -> bool {
9399
adt.is_variant_list_non_exhaustive()
94100
|| find_attr!(tcx.get_all_attrs(adt.did()), AttributeKind::NonExhaustive(..))
@@ -101,7 +107,7 @@ pub fn has_non_exhaustive_attr(tcx: TyCtxt<'_>, adt: AdtDef<'_>) -> bool {
101107
.any(|field_def| find_attr!(tcx.get_all_attrs(field_def.did), AttributeKind::NonExhaustive(..)))
102108
}
103109

104-
/// Checks if the given span contains a `#[cfg(..)]` attribute
110+
/// Checks whether the given span contains a `#[cfg(..)]` attribute
105111
pub fn span_contains_cfg(cx: &LateContext<'_>, s: Span) -> bool {
106112
s.check_source_text(cx, |src| {
107113
let mut iter = tokenize_with_text(src);
@@ -123,6 +129,8 @@ pub fn span_contains_cfg(cx: &LateContext<'_>, s: Span) -> bool {
123129
false
124130
})
125131
}
132+
133+
/// Currently used to keep track of the current value of `#[clippy::cognitive_complexity(N)]`
126134
pub struct LimitStack {
127135
default: u64,
128136
stack: Vec<u64>,
@@ -134,6 +142,7 @@ impl Drop for LimitStack {
134142
}
135143
}
136144

145+
#[expect(missing_docs, reason = "they're all trivial...")]
137146
impl LimitStack {
138147
#[must_use]
139148
/// Initialize the stack starting with a default value, which usually comes from configuration
@@ -157,7 +166,7 @@ impl LimitStack {
157166
}
158167

159168
fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[impl AttributeExt], name: Symbol, mut f: F) {
160-
for attr in get_attr(sess, attrs, name) {
169+
for attr in get_builtin_attr(sess, attrs, name) {
161170
let Some(value) = attr.value_str() else {
162171
sess.dcx().span_err(attr.span(), "bad clippy attribute");
163172
continue;

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)