give functions more descriptive names, add docs

This commit is contained in:
Ada Alakbarova
2025-09-25 19:54:17 +02:00
parent 0c013414b8
commit e55b435d99
7 changed files with 28 additions and 18 deletions
@@ -4,7 +4,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::{first_line_of_span, indent_of, snippet};
use clippy_utils::ty::{for_each_top_level_late_bound_region, is_copy};
use clippy_utils::{get_attr, is_lint_allowed, sym};
use clippy_utils::{get_builtin_attr, is_lint_allowed, sym};
use itertools::Itertools;
use rustc_ast::Mutability;
use rustc_data_structures::fx::FxIndexSet;
@@ -183,7 +183,7 @@ fn has_sig_drop_attr(&mut self, ty: Ty<'tcx>) -> bool {
fn has_sig_drop_attr_impl(&mut self, ty: Ty<'tcx>) -> bool {
if let Some(adt) = ty.ty_adt_def()
&& get_attr(
&& get_builtin_attr(
self.cx.sess(),
self.cx.tcx.get_all_attrs(adt.did()),
sym::has_significant_drop,
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::res::MaybeResPath;
use clippy_utils::source::{indent_of, snippet};
use clippy_utils::{expr_or_init, get_attr, peel_hir_expr_unary, sym};
use clippy_utils::{expr_or_init, get_builtin_attr, peel_hir_expr_unary, sym};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
@@ -167,7 +167,7 @@ fn has_sig_drop_attr(&mut self, ty: Ty<'tcx>, depth: usize) -> bool {
fn has_sig_drop_attr_uncached(&mut self, ty: Ty<'tcx>, depth: usize) -> bool {
if let Some(adt) = ty.ty_adt_def() {
let mut iter = get_attr(
let mut iter = get_builtin_attr(
self.cx.sess(),
self.cx.tcx.get_all_attrs(adt.did()),
sym::has_significant_drop,
+2 -2
View File
@@ -1,5 +1,5 @@
use clippy_utils::res::MaybeQPath;
use clippy_utils::{get_attr, higher, sym};
use clippy_utils::{get_builtin_attr, higher, sym};
use itertools::Itertools;
use rustc_ast::LitIntType;
use rustc_ast::ast::{LitFloatType, LitKind};
@@ -856,5 +856,5 @@ macro_rules! kind {
fn has_attr(cx: &LateContext<'_>, hir_id: HirId) -> bool {
let attrs = cx.tcx.hir_attrs(hir_id);
get_attr(cx.sess(), attrs, sym::author).count() > 0
get_builtin_attr(cx.sess(), attrs, sym::author).count() > 0
}
+2 -2
View File
@@ -1,4 +1,4 @@
use clippy_utils::{get_attr, sym};
use clippy_utils::{get_builtin_attr, sym};
use hir::TraitItem;
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -60,5 +60,5 @@ fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &hir::ImplItem<'_>) {
fn has_attr(cx: &LateContext<'_>, hir_id: hir::HirId) -> bool {
let attrs = cx.tcx.hir_attrs(hir_id);
get_attr(cx.sess(), attrs, sym::dump).count() > 0
get_builtin_attr(cx.sess(), attrs, sym::dump).count() > 0
}
+17 -8
View File
@@ -1,3 +1,5 @@
//! Utility functions for attributes, including Clippy's built-in ones
use crate::source::SpanRangeExt;
use crate::{sym, tokenize_with_text};
use rustc_ast::attr;
@@ -12,7 +14,8 @@
use rustc_span::{Span, Symbol};
use std::str::FromStr;
pub fn get_attr<'a, A: AttributeExt + 'a>(
/// Given `attrs`, extract all the instances of a built-in Clippy attribute called `name`
pub fn get_builtin_attr<'a, A: AttributeExt + 'a>(
sess: &'a Session,
attrs: &'a [A],
name: Symbol,
@@ -59,9 +62,11 @@ pub fn get_attr<'a, A: AttributeExt + 'a>(
})
}
pub fn get_unique_attr<'a, A: AttributeExt>(sess: &'a Session, attrs: &'a [A], name: Symbol) -> Option<&'a A> {
/// If `attrs` contain exactly one instance of a built-in Clippy attribute called `name`,
/// returns that attribute, and `None` otherwise
pub fn get_unique_builtin_attr<'a, A: AttributeExt>(sess: &'a Session, attrs: &'a [A], name: Symbol) -> Option<&'a A> {
let mut unique_attr: Option<&A> = None;
for attr in get_attr(sess, attrs, name) {
for attr in get_builtin_attr(sess, attrs, name) {
if let Some(duplicate) = unique_attr {
sess.dcx()
.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
unique_attr
}
/// Returns true if the attributes contain any of `proc_macro`,
/// `proc_macro_derive` or `proc_macro_attribute`, false otherwise
/// Checks whether `attrs` contain any of `proc_macro`, `proc_macro_derive` or
/// `proc_macro_attribute`
pub fn is_proc_macro(attrs: &[impl AttributeExt]) -> bool {
attrs.iter().any(AttributeExt::is_proc_macro_attr)
}
/// Returns true if the attributes contain `#[doc(hidden)]`
/// Checks whether `attrs` contain `#[doc(hidden)]`
pub fn is_doc_hidden(attrs: &[impl AttributeExt]) -> bool {
attrs
.iter()
@@ -89,6 +94,7 @@ pub fn is_doc_hidden(attrs: &[impl AttributeExt]) -> bool {
.any(|l| attr::list_contains_name(&l, sym::hidden))
}
/// Checks whether the given ADT, or any of its fields/variants, are marked as `#[non_exhaustive]`
pub fn has_non_exhaustive_attr(tcx: TyCtxt<'_>, adt: AdtDef<'_>) -> bool {
adt.is_variant_list_non_exhaustive()
|| 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 {
.any(|field_def| find_attr!(tcx.get_all_attrs(field_def.did), AttributeKind::NonExhaustive(..)))
}
/// Checks if the given span contains a `#[cfg(..)]` attribute
/// Checks whether the given span contains a `#[cfg(..)]` attribute
pub fn span_contains_cfg(cx: &LateContext<'_>, s: Span) -> bool {
s.check_source_text(cx, |src| {
let mut iter = tokenize_with_text(src);
@@ -123,6 +129,8 @@ pub fn span_contains_cfg(cx: &LateContext<'_>, s: Span) -> bool {
false
})
}
/// Currently used to keep track of the current value of `#[clippy::cognitive_complexity(N)]`
pub struct LimitStack {
default: u64,
stack: Vec<u64>,
@@ -134,6 +142,7 @@ fn drop(&mut self) {
}
}
#[expect(missing_docs, reason = "they're all trivial...")]
impl LimitStack {
#[must_use]
/// Initialize the stack starting with a default value, which usually comes from configuration
@@ -157,7 +166,7 @@ pub fn pop_attrs(&mut self, sess: &Session, attrs: &[impl AttributeExt], name: S
}
fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[impl AttributeExt], name: Symbol, mut f: F) {
for attr in get_attr(sess, attrs, name) {
for attr in get_builtin_attr(sess, attrs, name) {
let Some(value) = attr.value_str() else {
sess.dcx().span_err(attr.span(), "bad clippy attribute");
continue;
+1
View File
@@ -52,6 +52,7 @@
extern crate smallvec;
pub mod ast_utils;
#[deny(missing_docs)]
pub mod attrs;
mod check_proc_macro;
pub mod comparisons;
+2 -2
View File
@@ -3,7 +3,7 @@
use std::sync::{Arc, OnceLock};
use crate::visitors::{Descend, for_each_expr_without_closures};
use crate::{get_unique_attr, sym};
use crate::{get_unique_builtin_attr, sym};
use arrayvec::ArrayVec;
use rustc_ast::{FormatArgs, FormatArgument, FormatPlaceholder};
@@ -42,7 +42,7 @@ pub fn is_format_macro(cx: &LateContext<'_>, macro_def_id: DefId) -> bool {
} else {
// Allow users to tag any macro as being format!-like
// TODO: consider deleting FORMAT_MACRO_DIAG_ITEMS and using just this method
get_unique_attr(cx.sess(), cx.tcx.get_all_attrs(macro_def_id), sym::format_args).is_some()
get_unique_builtin_attr(cx.sess(), cx.tcx.get_all_attrs(macro_def_id), sym::format_args).is_some()
}
}