Skip to content

Commit fb46739

Browse files
committed
Auto merge of rust-lang#130615 - GuillaumeGomez:rollup-tq0ff7y, r=GuillaumeGomez
Rollup of 6 pull requests Successful merges: - rust-lang#129542 (Add regression test for rust-lang#129541) - rust-lang#129755 (test: cross-edition metavar fragment specifiers) - rust-lang#130566 (Break up compiletest `runtest.rs` into smaller helper modules) - rust-lang#130585 (Add tidy check for rustdoc templates to ensure the whitespace characters are all stripped) - rust-lang#130605 (Fix feature name in test) - rust-lang#130607 ([Clippy] Remove final std paths for diagnostic item) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 1a5a224 + b2bcdbc commit fb46739

29 files changed

+2413
-2177
lines changed

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ symbols! {
309309
RwLockReadGuard,
310310
RwLockWriteGuard,
311311
Saturating,
312+
SeekFrom,
312313
Send,
313314
SeqCst,
314315
Sized,

library/std/src/io/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,7 @@ pub trait Seek {
20582058
/// It is used by the [`Seek`] trait.
20592059
#[derive(Copy, PartialEq, Eq, Clone, Debug)]
20602060
#[stable(feature = "rust1", since = "1.0.0")]
2061+
#[cfg_attr(not(test), rustc_diagnostic_item = "SeekFrom")]
20612062
pub enum SeekFrom {
20622063
/// Sets the offset to the provided number of bytes.
20632064
#[stable(feature = "rust1", since = "1.0.0")]

src/tools/clippy/clippy_lints/src/methods/seek_from_current.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_span::sym;
88
use clippy_utils::diagnostics::span_lint_and_sugg;
99
use clippy_utils::source::snippet_with_applicability;
1010
use clippy_utils::ty::implements_trait;
11-
use clippy_utils::{match_def_path, paths};
11+
use clippy_utils::is_enum_variant_ctor;
1212

1313
use super::SEEK_FROM_CURRENT;
1414

@@ -36,8 +36,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'
3636
fn arg_is_seek_from_current<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
3737
if let ExprKind::Call(f, args) = expr.kind
3838
&& let ExprKind::Path(ref path) = f.kind
39-
&& let Some(def_id) = cx.qpath_res(path, f.hir_id).opt_def_id()
40-
&& match_def_path(cx, def_id, &paths::STD_IO_SEEK_FROM_CURRENT)
39+
&& let Some(ctor_call_id) = cx.qpath_res(path, f.hir_id).opt_def_id()
40+
&& is_enum_variant_ctor(cx, sym::SeekFrom, sym!(Current), ctor_call_id)
4141
{
4242
// check if argument of `SeekFrom::Current` is `0`
4343
if args.len() == 1

src/tools/clippy/clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs

+3-3
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::ty::implements_trait;
3-
use clippy_utils::{is_expr_used_or_unified, match_def_path, paths};
3+
use clippy_utils::{is_expr_used_or_unified, is_enum_variant_ctor};
44
use rustc_ast::ast::{LitIntType, LitKind};
55
use rustc_data_structures::packed::Pu128;
66
use rustc_errors::Applicability;
@@ -28,8 +28,8 @@ pub(super) fn check<'tcx>(
2828
&& implements_trait(cx, ty, seek_trait_id, &[])
2929
&& let ExprKind::Call(func, args1) = arg.kind
3030
&& let ExprKind::Path(ref path) = func.kind
31-
&& let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
32-
&& match_def_path(cx, def_id, &paths::STD_IO_SEEKFROM_START)
31+
&& let Some(ctor_call_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
32+
&& is_enum_variant_ctor(cx, sym::SeekFrom, sym!(Start), ctor_call_id)
3333
&& args1.len() == 1
3434
&& let ExprKind::Lit(lit) = args1[0].kind
3535
&& let LitKind::Int(Pu128(0), LitIntType::Unsuffixed) = lit.node

src/tools/clippy/clippy_utils/src/lib.rs

+11-17
Original file line numberDiff line numberDiff line change
@@ -263,24 +263,18 @@ pub fn is_res_lang_ctor(cx: &LateContext<'_>, res: Res, lang_item: LangItem) ->
263263
}
264264
}
265265

266-
pub fn is_res_diagnostic_ctor(cx: &LateContext<'_>, res: Res, diag_item: Symbol) -> bool {
267-
if let Res::Def(DefKind::Ctor(..), id) = res
268-
&& let Some(id) = cx.tcx.opt_parent(id)
269-
{
270-
cx.tcx.is_diagnostic_item(diag_item, id)
271-
} else {
272-
false
273-
}
274-
}
275266

276-
/// Checks if a `QPath` resolves to a constructor of a diagnostic item.
277-
pub fn is_diagnostic_ctor(cx: &LateContext<'_>, qpath: &QPath<'_>, diagnostic_item: Symbol) -> bool {
278-
if let QPath::Resolved(_, path) = qpath {
279-
if let Res::Def(DefKind::Ctor(..), ctor_id) = path.res {
280-
return cx.tcx.is_diagnostic_item(diagnostic_item, cx.tcx.parent(ctor_id));
281-
}
282-
}
283-
false
267+
/// Checks if `{ctor_call_id}(...)` is `{enum_item}::{variant_name}(...)`.
268+
pub fn is_enum_variant_ctor(cx: &LateContext<'_>, enum_item: Symbol, variant_name: Symbol, ctor_call_id: DefId) -> bool {
269+
let Some(enum_def_id) = cx.tcx.get_diagnostic_item(enum_item) else {
270+
return false;
271+
};
272+
273+
let variants = cx.tcx.adt_def(enum_def_id).variants().iter();
274+
variants
275+
.filter(|variant| variant.name == variant_name)
276+
.filter_map(|variant| variant.ctor.as_ref())
277+
.any(|(_, ctor_def_id)| *ctor_def_id == ctor_call_id)
284278
}
285279

286280
/// Checks if the `DefId` matches the given diagnostic item or it's constructor.

src/tools/clippy/clippy_utils/src/paths.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ pub const SYM_MODULE: [&str; 3] = ["rustc_span", "symbol", "sym"];
2929
pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"];
3030

3131
// Paths in `core`/`alloc`/`std`. This should be avoided and cleaned up by adding diagnostic items.
32-
pub const STD_IO_SEEK_FROM_CURRENT: [&str; 4] = ["std", "io", "SeekFrom", "Current"];
33-
pub const STD_IO_SEEKFROM_START: [&str; 4] = ["std", "io", "SeekFrom", "Start"];
32+
// ... none currently!
3433

3534
// Paths in clippy itself
3635
pub const MSRV: [&str; 3] = ["clippy_config", "msrvs", "Msrv"];

0 commit comments

Comments
 (0)