Skip to content

Commit d734981

Browse files
committed
Introduce hir::ImplItemImplKind
1 parent d419d51 commit d734981

File tree

5 files changed

+45
-29
lines changed

5 files changed

+45
-29
lines changed

clippy_lints/src/disallowed_macros.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_data_structures::fx::FxHashSet;
88
use rustc_hir::def::DefKind;
99
use rustc_hir::def_id::DefIdMap;
1010
use rustc_hir::{
11-
AmbigArg, Expr, ExprKind, ForeignItem, HirId, ImplItem, Item, ItemKind, OwnerId, Pat, Path, Stmt, TraitItem, Ty,
11+
AmbigArg, Expr, ExprKind, ForeignItem, HirId, ImplItem, ImplItemImplKind, Item, ItemKind, OwnerId, Pat, Path, Stmt, TraitItem, Ty,
1212
};
1313
use rustc_lint::{LateContext, LateLintPass};
1414
use rustc_middle::ty::TyCtxt;
@@ -177,7 +177,9 @@ impl LateLintPass<'_> for DisallowedMacros {
177177

178178
fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &ImplItem<'_>) {
179179
self.check(cx, item.span, None);
180-
self.check(cx, item.vis_span, None);
180+
if let ImplItemImplKind::Inherent { vis_span, .. } = item.impl_kind {
181+
self.check(cx, vis_span, None);
182+
}
181183
}
182184

183185
fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) {

clippy_lints/src/manual_async_fn.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_errors::Applicability;
44
use rustc_hir::intravisit::FnKind;
55
use rustc_hir::{
66
Block, Body, Closure, ClosureKind, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind, FnDecl,
7-
FnRetTy, GenericBound, ImplItem, Item, Node, OpaqueTy, TraitRef, Ty, TyKind,
7+
FnRetTy, GenericBound, Node, OpaqueTy, TraitRef, Ty, TyKind,
88
};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::middle::resolve_bound_vars::ResolvedArg;
@@ -60,8 +60,11 @@ impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn {
6060
&& let ExprKind::Block(block, _) = body.value.kind
6161
&& block.stmts.is_empty()
6262
&& let Some(closure_body) = desugared_async_block(cx, block)
63-
&& let Node::Item(Item {vis_span, ..}) | Node::ImplItem(ImplItem {vis_span, ..}) =
64-
cx.tcx.hir_node_by_def_id(fn_def_id)
63+
&& let Some(vis_span_opt) = match cx.tcx.hir_node_by_def_id(fn_def_id) {
64+
Node::Item(item) => Some(Some(item.vis_span)),
65+
Node::ImplItem(impl_item) => Some(impl_item.vis_span()),
66+
_ => None,
67+
}
6568
&& !span.from_expansion()
6669
{
6770
let header_span = span.with_hi(ret_ty.span.hi());
@@ -72,7 +75,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn {
7275
header_span,
7376
"this function can be simplified using the `async fn` syntax",
7477
|diag| {
75-
if let Some(vis_snip) = vis_span.get_source_text(cx)
78+
if let Some(vis_span) = vis_span_opt
79+
&& let Some(vis_snip) = vis_span.get_source_text(cx)
7680
&& let Some(header_snip) = header_span.get_source_text(cx)
7781
&& let Some(ret_pos) = position_before_rarrow(&header_snip)
7882
&& let Some((_, ret_snip)) = suggested_ret(cx, output)

clippy_lints/src/min_ident_chars.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use rustc_data_structures::fx::FxHashSet;
55
use rustc_hir::def::{DefKind, Res};
66
use rustc_hir::intravisit::{Visitor, walk_item, walk_trait_item};
77
use rustc_hir::{
8-
GenericParamKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, ItemLocalId, Node, Pat, PatKind, TraitItem,
9-
UsePath,
8+
GenericParamKind, HirId, Impl, ImplItem, ImplItemImplKind, ImplItemKind, Item, ItemKind, ItemLocalId, Node, Pat,
9+
PatKind, TraitItem, UsePath,
1010
};
1111
use rustc_lint::{LateContext, LateLintPass, LintContext};
1212
use rustc_session::impl_lint_pass;
@@ -256,7 +256,7 @@ fn is_not_in_trait_impl(cx: &LateContext<'_>, pat: &Pat<'_>, ident: Ident) -> bo
256256
}
257257

258258
fn get_param_name(impl_item: &ImplItem<'_>, cx: &LateContext<'_>, ident: Ident) -> Option<Symbol> {
259-
if let Some(trait_item_def_id) = impl_item.trait_item_def_id {
259+
if let ImplItemImplKind::Trait { trait_item_def_id: Ok(trait_item_def_id), .. } = impl_item.impl_kind {
260260
let trait_param_names = cx.tcx.fn_arg_idents(trait_item_def_id);
261261

262262
let ImplItemKind::Fn(_, body_id) = impl_item.kind else {

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,23 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
158158
let mir = cx.tcx.optimized_mir(def_id);
159159

160160
if let Ok(()) = is_min_const_fn(cx, mir, self.msrv)
161-
&& let hir::Node::Item(hir::Item { vis_span, .. }) | hir::Node::ImplItem(hir::ImplItem { vis_span, .. }) =
162-
cx.tcx.hir_node_by_def_id(def_id)
161+
&& let node = cx.tcx.hir_node_by_def_id(def_id)
162+
&& let Some((item_span, vis_span_opt)) = match node {
163+
hir::Node::Item(item) => Some((item.span, Some(item.vis_span))),
164+
hir::Node::ImplItem(impl_item) => Some((impl_item.span, impl_item.vis_span())),
165+
_ => None,
166+
}
163167
{
164-
let suggestion = if vis_span.is_empty() { "const " } else { " const" };
168+
let (sugg_span, suggestion) = if let Some(vis_span) = vis_span_opt
169+
&& !vis_span.is_empty()
170+
{
171+
(vis_span.shrink_to_hi(), " const")
172+
} else {
173+
(item_span.shrink_to_lo(), "const ")
174+
};
165175
span_lint_and_then(cx, MISSING_CONST_FOR_FN, span, "this could be a `const fn`", |diag| {
166176
diag.span_suggestion_verbose(
167-
vis_span.shrink_to_hi(),
177+
sugg_span,
168178
"make the function `const`",
169179
suggestion,
170180
Applicability::MachineApplicable,

clippy_utils/src/check_proc_macro.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_ast::token::CommentKind;
1919
use rustc_hir::intravisit::FnKind;
2020
use rustc_hir::{
2121
Block, BlockCheckMode, Body, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, FnRetTy, HirId, Impl,
22-
ImplItem, ImplItemKind, IsAuto, Item, ItemKind, Lit, LoopSource, MatchSource, MutTy, Node, Path, QPath, Safety,
22+
ImplItem, ImplItemImplKind, ImplItemKind, IsAuto, Item, ItemKind, Lit, LoopSource, MatchSource, MutTy, Node, Path, QPath, Safety,
2323
TraitImplHeader, TraitItem, TraitItemKind, Ty, TyKind, UnOp, UnsafeSource, Variant, VariantData, YieldSource,
2424
};
2525
use rustc_lint::{EarlyContext, LateContext, LintContext};
@@ -280,16 +280,17 @@ fn trait_item_search_pat(item: &TraitItem<'_>) -> (Pat, Pat) {
280280
}
281281

282282
fn impl_item_search_pat(item: &ImplItem<'_>) -> (Pat, Pat) {
283-
let (start_pat, end_pat) = match &item.kind {
283+
let (mut start_pat, end_pat) = match &item.kind {
284284
ImplItemKind::Const(..) => (Pat::Str("const"), Pat::Str(";")),
285285
ImplItemKind::Type(..) => (Pat::Str("type"), Pat::Str(";")),
286286
ImplItemKind::Fn(sig, ..) => (fn_header_search_pat(sig.header), Pat::Str("")),
287287
};
288-
if item.vis_span.is_empty() {
289-
(start_pat, end_pat)
290-
} else {
291-
(Pat::Str("pub"), end_pat)
292-
}
288+
if let ImplItemImplKind::Inherent { vis_span, .. } = item.impl_kind
289+
&& !vis_span.is_empty()
290+
{
291+
start_pat = Pat::Str("pub");
292+
};
293+
(start_pat, end_pat)
293294
}
294295

295296
fn field_def_search_pat(def: &FieldDef<'_>) -> (Pat, Pat) {
@@ -313,21 +314,20 @@ fn variant_search_pat(v: &Variant<'_>) -> (Pat, Pat) {
313314
}
314315

315316
fn fn_kind_pat(tcx: TyCtxt<'_>, kind: &FnKind<'_>, body: &Body<'_>, hir_id: HirId) -> (Pat, Pat) {
316-
let (start_pat, end_pat) = match kind {
317+
let (mut start_pat, end_pat) = match kind {
317318
FnKind::ItemFn(.., header) => (fn_header_search_pat(*header), Pat::Str("")),
318319
FnKind::Method(.., sig) => (fn_header_search_pat(sig.header), Pat::Str("")),
319320
FnKind::Closure => return (Pat::Str(""), expr_search_pat(tcx, body.value).1),
320321
};
321-
let start_pat = match tcx.hir_node(hir_id) {
322-
Node::Item(Item { vis_span, .. }) | Node::ImplItem(ImplItem { vis_span, .. }) => {
323-
if vis_span.is_empty() {
324-
start_pat
325-
} else {
326-
Pat::Str("pub")
322+
match tcx.hir_node(hir_id) {
323+
Node::Item(Item { vis_span, .. })
324+
| Node::ImplItem(ImplItem { impl_kind: ImplItemImplKind::Inherent { vis_span, .. }, .. }) => {
325+
if !vis_span.is_empty() {
326+
start_pat = Pat::Str("pub")
327327
}
328328
},
329-
Node::TraitItem(_) => start_pat,
330-
_ => Pat::Str(""),
329+
Node::ImplItem(_) | Node::TraitItem(_) => {},
330+
_ => start_pat = Pat::Str(""),
331331
};
332332
(start_pat, end_pat)
333333
}

0 commit comments

Comments
 (0)