Skip to content

Clippy subtree update #142451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 94 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
d49b8aa
Remove `ClippyCtfe` pass
Alexendoo Apr 30, 2025
a9225f8
Updated feature flag and output of `clippy/tests/ui/map_flatten*`
May 16, 2025
0524773
fix: `unit_arg` suggests wrongly for `Default::default`
profetia May 24, 2025
ebf39a5
fix: `unit_arg` wrongly handled macros
profetia May 24, 2025
d82e51b
Test that all interned symbols are referenced in Clippy sources
samueltardieu May 18, 2025
f6e5fa2
remove eq_unspanned from TokenStream
chenyukang May 26, 2025
8e581e3
add lint infallible_try_from
ComputerDruid May 15, 2025
e177dd0
Reduce `P<T>` to a typedef of `Box<T>`.
nnethercote May 26, 2025
1c070ca
fix: `branches_sharing_code` suggests wrongly when dealing with macros
profetia May 28, 2025
4f3d529
Remove incremental test, disable incremental for cargo tests
Alexendoo May 28, 2025
3c8bfd1
`zombie_processes`: do not complain about early early returns
samueltardieu May 28, 2025
459280c
Reorder fields in `hir::ItemKind` variants.
nnethercote May 28, 2025
e64dd8e
new restriction lint: pointer_format
llogiq May 13, 2025
9f86a57
Merge commit '57cbadd68ac473bc50453f6b1320a02b68115f12'
flip1995 May 31, 2025
3ed210c
Use interned symbols instead of strings in more places
samueltardieu May 20, 2025
43b7d87
`missing_const_for_fn`: consider constness of instance
samueltardieu May 8, 2025
61465ed
Rollup merge of #141072 - Rynibami:stabilize-const-result-flatten, r=…
jhpratt May 31, 2025
8029208
Lint more cases in `unit_arg`
profetia May 31, 2025
1652187
new restriction lint: pointer_format (#14792)
samueltardieu Jun 1, 2025
d4aed51
Do not count deprecated lints in lint total
samueltardieu Jun 1, 2025
3f48a2b
Use interned symbols instead of strings in more places (#14855)
y21 Jun 1, 2025
466e0fb
Do not count deprecated lints in lint total (#14948)
llogiq Jun 1, 2025
89dc07a
chore: fix smart quote in comment used by update_lints script
krikera Jun 1, 2025
a5a7eea
chore: replace smart quote with straight quote in update_lints commen…
Centri3 Jun 1, 2025
abf5d4c
Invert the sense of `is_not_macro_export`.
nnethercote May 29, 2025
12fc6c7
fix: `semicolon_outside_block` suggests wrongly when inside macros
profetia Jun 2, 2025
88bb8d1
Fix `semicolon_outside_block` suggests wrongly when inside macros (#1…
samueltardieu Jun 2, 2025
a4360d6
fix: `unnecessary_debug_formatting` FP inside `Debug` impl
profetia Jun 2, 2025
225abd6
Use `None::<u64>` instead of `0` to denote an absence of value
samueltardieu Jun 2, 2025
d7b27ec
add lint `infallible_try_from` (#14813)
llogiq Jun 2, 2025
6fa2866
Use `None::<u64>` instead of `0` to denote an absence of value (#14957)
llogiq Jun 2, 2025
8708e3f
Auto merge of #141814 - flip1995:clippy-subtree-update, r=Manishearth
bors Jun 2, 2025
9b8c42c
Test that all interned symbols are referenced in Clippy sources (#14842)
Alexendoo Jun 2, 2025
166f664
Use the informative error as the main const eval error message
oli-obk May 28, 2025
d1f9d02
Clarify why we are talking about a failed const eval at a random place
oli-obk May 30, 2025
b51e737
Introduce coerce_container_to_any
Ralith May 15, 2025
fcfbcc8
doc_suspicious_footnotes: lint text that looks like a footnote
notriddle Apr 29, 2025
1b2b7e6
Overhaul `UsePath`.
nnethercote May 29, 2025
4effc58
Rollup merge of #141741 - nnethercote:overhaul-UsePath, r=petrochenkov
matthiaskrgr Jun 3, 2025
8d8b3f1
Add new lint: `ip_consant`
relaxcn May 23, 2025
716eadf
`zombie_processes`: do not complain about early early returns (#14912)
Jarcho Jun 3, 2025
eafef84
`missing_const_for_fn`: consider constness of instance (#14759)
Jarcho Jun 3, 2025
3b64c5c
Lint reversed ordering in partial ord impl
donkomura Jun 1, 2025
a4debd2
Lint reversed ordering in partial ord impl (#14945)
samueltardieu Jun 3, 2025
ed226d2
Fix false positive for unused_unit
Jun 3, 2025
1e96456
Reference correct PR
Jun 3, 2025
2a5a2fa
Use interned strings when possible, for efficiency purposes
samueltardieu Jun 3, 2025
a0b0f91
Rollup merge of #141698 - oli-obk:ctfe-err-flip, r=RalfJung
matthiaskrgr Jun 3, 2025
84ef7fb
Use interned strings when possible, for efficiency purposes (#14963)
llogiq Jun 4, 2025
6ed003d
fix suggestion-causes-error of `print_literal` and `write_literal`
relaxcn Jun 3, 2025
1ae37ad
Do not recurse indefinitely while checking for inner mutability
samueltardieu Jun 4, 2025
ed143af
fix suggestion-causes-error of `print_literal` and `write_literal` (#…
samueltardieu Jun 4, 2025
9b8bf53
Rollup merge of #141570 - chenyukang:yukang-fix-eq_unspanned, r=worki…
matthiaskrgr Jun 4, 2025
8b59e34
Replace `elided_named_lifetimes` with `mismatched_lifetime_syntaxes`
shepmaster Mar 17, 2025
c9a97e1
Do not lint macro generated codes
donkomura Jun 4, 2025
a76c2b3
Fix `unnecessary_debug_formatting` FP inside `Debug` impl (#14955)
dswij Jun 4, 2025
29492da
Fix suggestion-causes-error of `manual_swap`
relaxcn Jun 4, 2025
c90c7c4
doc_suspicious_footnotes: lint text that looks like a footnote (#14708)
blyxyas Jun 5, 2025
9f9f993
Replace some `Option<Span>` with `Span` and use DUMMY_SP instead of None
oli-obk Jun 4, 2025
cf75dd4
Remove incremental test, disable incremental for cargo tests (#14910)
dswij Jun 5, 2025
f8a3929
Do not lint macro generated codes (#14976)
Alexendoo Jun 5, 2025
6ef2a2d
add new lint: `ip_constant` (#14878)
llogiq Jun 5, 2025
b379d54
Introduce coerce_container_to_any (#14812)
llogiq Jun 5, 2025
da93448
Do not recurse indefinitely while checking for inner mutability (#14965)
llogiq Jun 5, 2025
6bb0c97
Fix false positive for unused_unit (#14962)
llogiq Jun 5, 2025
2f80ead
Auto merge of #138677 - shepmaster:consistent-elided-lifetime-syntax,…
bors Jun 5, 2025
2c95105
Rollup merge of #142012 - oli-obk:no-optional-spans, r=fee1-dead
matthiaskrgr Jun 5, 2025
5930e54
fix typo
ada4a Jun 6, 2025
f2922e7
fix typo (#14995)
samueltardieu Jun 6, 2025
507183d
Rollup merge of #141603 - nnethercote:reduce-P, r=fee1-dead
GuillaumeGomez Jun 6, 2025
33052be
Fix `branches_sharing_code` suggests wrongly when dealing with macros…
dswij Jun 7, 2025
aace547
Add missing word to `cloned_ref_to_slice_refs` docs
cenviity Jun 7, 2025
0138c79
Add missing word to `cloned_ref_to_slice_refs` docs (#15012)
Centri3 Jun 8, 2025
4d6254c
fix: `create_dir` ignores paths in suggestions
profetia Jun 7, 2025
f397a30
Invert suggestion if pointer is tested for non-nullness
samueltardieu Jun 8, 2025
a1a1393
Invert suggestion if pointer is tested for non-nullness (#15015)
llogiq Jun 8, 2025
d98070d
fix: `std_instead_of_core` FP when part of the `use` cannot be replaced
profetia Jun 8, 2025
737b8be
Fix `std_instead_of_core` FP when part of the `use` cannot be replace…
llogiq Jun 8, 2025
b5eac24
fix: `match_single_binding` misses curlies on type signatures
profetia Jun 8, 2025
3af333e
Fix `create_dir` ignores paths in suggestions (#15011)
Alexendoo Jun 8, 2025
9e7782b
Fix `match_single_binding` misses curlies on type signatures (#15017)
llogiq Jun 8, 2025
4389e17
optimize codes
relaxcn Jun 9, 2025
358b80c
Fix `manual_flatten` removes the useless if let
donkomura May 21, 2025
6d7c16a
Remove `ClippyCtfe` pass (#14712)
blyxyas Jun 11, 2025
ce8d263
Fixes `manual_flatten` removes the useless if let (#14861)
samueltardieu Jun 12, 2025
180adb3
Fix `unit_arg` suggests wrongly for `Default::default` (#14881)
samueltardieu Jun 12, 2025
59d8345
Fix suggestion-causes-error of `manual_swap` (#14978)
samueltardieu Jun 12, 2025
6cd55b9
Optimize by 99.75% strlen_on_c_strings, 8.5% globally
blyxyas Jun 12, 2025
6662aed
[PERF] Optimize 3rd heaviest func, (81b -> 10m) (#15043)
samueltardieu Jun 13, 2025
85655d4
Merge remote-tracking branch 'upstream/master' into rustup
flip1995 Jun 13, 2025
19c2f03
Bump nightly version -> 2025-06-13
flip1995 Jun 13, 2025
4ef7529
Rustup (#15044)
flip1995 Jun 13, 2025
8fe9c2c
Merge commit '4ef75291b5dd6739212f1f61666d19d4e086690d' into clippy-s…
flip1995 Jun 13, 2025
9cb0a64
Move COERCE_CONTAINER_TO_ANY to nursery, as it has FPs
flip1995 Jun 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/tools/clippy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5685,6 +5685,7 @@ Released 2018-09-13
[`cmp_nan`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_nan
[`cmp_null`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_null
[`cmp_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_owned
[`coerce_container_to_any`]: https://rust-lang.github.io/rust-clippy/master/index.html#coerce_container_to_any
[`cognitive_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity
[`collapsible_else_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_else_if
[`collapsible_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
Expand Down Expand Up @@ -5736,6 +5737,7 @@ Released 2018-09-13
[`doc_markdown`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
[`doc_nested_refdefs`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_nested_refdefs
[`doc_overindented_list_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_overindented_list_items
[`doc_suspicious_footnotes`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_suspicious_footnotes
[`double_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_comparisons
[`double_ended_iterator_last`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_ended_iterator_last
[`double_must_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_must_use
Expand Down Expand Up @@ -5862,6 +5864,7 @@ Released 2018-09-13
[`ineffective_open_options`]: https://rust-lang.github.io/rust-clippy/master/index.html#ineffective_open_options
[`inefficient_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#inefficient_to_string
[`infallible_destructuring_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#infallible_destructuring_match
[`infallible_try_from`]: https://rust-lang.github.io/rust-clippy/master/index.html#infallible_try_from
[`infinite_iter`]: https://rust-lang.github.io/rust-clippy/master/index.html#infinite_iter
[`infinite_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#infinite_loop
[`inherent_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string
Expand All @@ -5888,6 +5891,7 @@ Released 2018-09-13
[`inverted_saturating_sub`]: https://rust-lang.github.io/rust-clippy/master/index.html#inverted_saturating_sub
[`invisible_characters`]: https://rust-lang.github.io/rust-clippy/master/index.html#invisible_characters
[`io_other_error`]: https://rust-lang.github.io/rust-clippy/master/index.html#io_other_error
[`ip_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#ip_constant
[`is_digit_ascii_radix`]: https://rust-lang.github.io/rust-clippy/master/index.html#is_digit_ascii_radix
[`items_after_statements`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements
[`items_after_test_module`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_test_module
Expand Down Expand Up @@ -6163,6 +6167,7 @@ Released 2018-09-13
[`pathbuf_init_then_push`]: https://rust-lang.github.io/rust-clippy/master/index.html#pathbuf_init_then_push
[`pattern_type_mismatch`]: https://rust-lang.github.io/rust-clippy/master/index.html#pattern_type_mismatch
[`permissions_set_readonly_false`]: https://rust-lang.github.io/rust-clippy/master/index.html#permissions_set_readonly_false
[`pointer_format`]: https://rust-lang.github.io/rust-clippy/master/index.html#pointer_format
[`pointers_in_nomem_asm_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#pointers_in_nomem_asm_block
[`positional_named_format_parameters`]: https://rust-lang.github.io/rust-clippy/master/index.html#positional_named_format_parameters
[`possible_missing_comma`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma
Expand Down
4 changes: 2 additions & 2 deletions src/tools/clippy/clippy_dev/src/update_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ pub fn generate_lint_files(
(
"clippy_lints/src/lib.rs",
&mut update_text_region_fn(
"// begin lints modules, do not remove this comment, its used in `update_lints`\n",
"// end lints modules, do not remove this comment, its used in `update_lints`",
"// begin lints modules, do not remove this comment, it's used in `update_lints`\n",
"// end lints modules, do not remove this comment, it's used in `update_lints`",
|dst| {
for lint_mod in lints.iter().map(|l| &l.module).sorted().dedup() {
writeln!(dst, "mod {lint_mod};").unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl ArbitrarySourceItemOrdering {
self.assoc_types_order
),
Some(before_item.span),
format!("should be placed before `{}`", before_item.ident.as_str(),),
format!("should be placed before `{}`", before_item.ident.name),
);
}

Expand All @@ -216,7 +216,7 @@ impl ArbitrarySourceItemOrdering {
ident.span,
"incorrect ordering of items (must be alphabetically ordered)",
Some(before_ident.span),
format!("should be placed before `{}`", before_ident.as_str(),),
format!("should be placed before `{}`", before_ident.name),
);
}

Expand All @@ -228,7 +228,7 @@ impl ArbitrarySourceItemOrdering {
};

let (before_span, note) = if let Some(ident) = before_item.kind.ident() {
(ident.span, format!("should be placed before `{}`", ident.as_str(),))
(ident.span, format!("should be placed before `{}`", ident.name))
} else {
(
before_item.span,
Expand All @@ -255,7 +255,7 @@ impl ArbitrarySourceItemOrdering {
self.assoc_types_order
),
Some(before_item.span),
format!("should be placed before `{}`", before_item.ident.as_str(),),
format!("should be placed before `{}`", before_item.ident.name),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(super) fn check_clippy(cx: &EarlyContext<'_>, attr: &Attribute) {

fn check_deprecated_cfg_recursively(cx: &EarlyContext<'_>, attr: &rustc_ast::MetaItem) {
if let Some(ident) = attr.ident() {
if ["any", "all", "not"].contains(&ident.name.as_str()) {
if matches!(ident.name, sym::any | sym::all | sym::not) {
let Some(list) = attr.meta_item_list() else { return };
for item in list.iter().filter_map(|item| item.meta_item()) {
check_deprecated_cfg_recursively(cx, item);
Expand Down
18 changes: 12 additions & 6 deletions src/tools/clippy/clippy_lints/src/attrs/duplicated_attributes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::DUPLICATED_ATTRIBUTES;
use clippy_utils::diagnostics::span_lint_and_then;
use itertools::Itertools;
use rustc_ast::{Attribute, MetaItem};
use rustc_data_structures::fx::FxHashMap;
use rustc_lint::EarlyContext;
use rustc_span::{Span, sym};
use rustc_span::{Span, Symbol, sym};
use std::collections::hash_map::Entry;

fn emit_if_duplicated(
Expand All @@ -29,7 +30,7 @@ fn check_duplicated_attr(
cx: &EarlyContext<'_>,
attr: &MetaItem,
attr_paths: &mut FxHashMap<String, Span>,
parent: &mut Vec<String>,
parent: &mut Vec<Symbol>,
) {
if attr.span.from_expansion() {
return;
Expand All @@ -43,25 +44,30 @@ fn check_duplicated_attr(
return;
}
if let Some(direct_parent) = parent.last()
&& direct_parent == sym::cfg_trace.as_str()
&& *direct_parent == sym::cfg_trace
&& [sym::all, sym::not, sym::any].contains(&name)
{
// FIXME: We don't correctly check `cfg`s for now, so if it's more complex than just a one
// level `cfg`, we leave.
return;
}
if let Some(value) = attr.value_str() {
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{name}={value}", parent.join(":")));
emit_if_duplicated(
cx,
attr,
attr_paths,
format!("{}:{name}={value}", parent.iter().join(":")),
);
} else if let Some(sub_attrs) = attr.meta_item_list() {
parent.push(name.as_str().to_string());
parent.push(name);
for sub_attr in sub_attrs {
if let Some(meta) = sub_attr.meta_item() {
check_duplicated_attr(cx, meta, attr_paths, parent);
}
}
parent.pop();
} else {
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{name}", parent.join(":")));
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{name}", parent.iter().join(":")));
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/tools/clippy/clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
use clippy_utils::eq_expr_value;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::SpanRangeExt;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
use clippy_utils::{eq_expr_value, sym};
use rustc_ast::ast::LitKind;
use rustc_attr_data_structures::RustcVersion;
use rustc_errors::Applicability;
Expand All @@ -13,7 +13,7 @@ use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
use rustc_lint::{LateContext, LateLintPass, Level};
use rustc_session::impl_lint_pass;
use rustc_span::def_id::LocalDefId;
use rustc_span::{Span, SyntaxContext, sym};
use rustc_span::{Span, Symbol, SyntaxContext};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -73,10 +73,10 @@ declare_clippy_lint! {
}

// For each pairs, both orders are considered.
const METHODS_WITH_NEGATION: [(Option<RustcVersion>, &str, &str); 3] = [
(None, "is_some", "is_none"),
(None, "is_err", "is_ok"),
(Some(msrvs::IS_NONE_OR), "is_some_and", "is_none_or"),
const METHODS_WITH_NEGATION: [(Option<RustcVersion>, Symbol, Symbol); 3] = [
(None, sym::is_some, sym::is_none),
(None, sym::is_err, sym::is_ok),
(Some(msrvs::IS_NONE_OR), sym::is_some_and, sym::is_none_or),
];

pub struct NonminimalBool {
Expand Down Expand Up @@ -440,9 +440,7 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: Msrv, expr: &Expr<'_>) -> Optio
.iter()
.copied()
.flat_map(|(msrv, a, b)| vec![(msrv, a, b), (msrv, b, a)])
.find(|&(msrv, a, _)| {
a == path.ident.name.as_str() && msrv.is_none_or(|msrv| curr_msrv.meets(cx, msrv))
})
.find(|&(msrv, a, _)| a == path.ident.name && msrv.is_none_or(|msrv| curr_msrv.meets(cx, msrv)))
.and_then(|(_, _, neg_method)| {
let negated_args = args
.iter()
Expand Down
19 changes: 9 additions & 10 deletions src/tools/clippy/clippy_lints/src/casts/cast_lossless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,28 @@ fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: M
return false;
}

match (cast_from.is_integral(), cast_to.is_integral()) {
(true, true) => {
match (
utils::int_ty_to_nbits(cx.tcx, cast_from),
utils::int_ty_to_nbits(cx.tcx, cast_to),
) {
(Some(from_nbits), Some(to_nbits)) => {
let cast_signed_to_unsigned = cast_from.is_signed() && !cast_to.is_signed();
let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
!is_isize_or_usize(cast_from)
&& !is_isize_or_usize(cast_to)
&& from_nbits < to_nbits
&& !cast_signed_to_unsigned
},

(true, false) => {
let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
(Some(from_nbits), None) => {
// FIXME: handle `f16` and `f128`
let to_nbits = if let ty::Float(FloatTy::F32) = cast_to.kind() {
32
} else {
64
};
!is_isize_or_usize(cast_from) && from_nbits < to_nbits
},
(false, true) if matches!(cast_from.kind(), ty::Bool) && msrv.meets(cx, msrvs::FROM_BOOL) => true,
(_, _) => {
matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64))
},
(None, Some(_)) if cast_from.is_bool() && msrv.meets(cx, msrvs::FROM_BOOL) => true,
_ => matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64)),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,14 @@ pub(super) fn check(
cast_to: Ty<'_>,
cast_to_span: Span,
) {
let msg = match (cast_from.kind(), cast_to.is_integral()) {
(ty::Int(_) | ty::Uint(_), true) => {
let msg = match (cast_from.kind(), utils::int_ty_to_nbits(cx.tcx, cast_to)) {
(ty::Int(_) | ty::Uint(_), Some(to_nbits)) => {
let from_nbits = apply_reductions(
cx,
utils::int_ty_to_nbits(cast_from, cx.tcx),
utils::int_ty_to_nbits(cx.tcx, cast_from).unwrap(),
cast_expr,
cast_from.is_signed(),
);
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);

let (should_lint, suffix) = match (is_isize_or_usize(cast_from), is_isize_or_usize(cast_to)) {
(true, true) | (false, false) => (to_nbits < from_nbits, ""),
Expand All @@ -121,7 +120,7 @@ pub(super) fn check(
format!("casting `{cast_from}` to `{cast_to}` may truncate the value{suffix}",)
},

(ty::Adt(def, _), true) if def.is_enum() => {
(ty::Adt(def, _), Some(to_nbits)) if def.is_enum() => {
let (from_nbits, variant) = if let ExprKind::Path(p) = &cast_expr.kind
&& let Res::Def(DefKind::Ctor(..), id) = cx.qpath_res(p, cast_expr.hir_id)
{
Expand All @@ -132,7 +131,6 @@ pub(super) fn check(
} else {
(utils::enum_ty_to_nbits(*def, cx.tcx), None)
};
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);

let cast_from_ptr_size = def.repr().int.is_none_or(|ty| matches!(ty, IntegerType::Pointer(_),));
let suffix = match (cast_from_ptr_size, is_isize_or_usize(cast_to)) {
Expand All @@ -157,11 +155,11 @@ pub(super) fn check(
format!("casting `{cast_from}` to `{cast_to}` may truncate the value{suffix}")
},

(ty::Float(_), true) => {
(ty::Float(_), Some(_)) => {
format!("casting `{cast_from}` to `{cast_to}` may truncate the value")
},

(ty::Float(FloatTy::F64), false) if matches!(cast_to.kind(), &ty::Float(FloatTy::F32)) => {
(ty::Float(FloatTy::F64), None) if matches!(cast_to.kind(), &ty::Float(FloatTy::F32)) => {
"casting `f64` to `f32` may truncate the value".to_string()
},

Expand Down
10 changes: 5 additions & 5 deletions src/tools/clippy/clippy_lints/src/casts/cast_possible_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ enum EmitState {
}

pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
if !(cast_from.is_integral() && cast_to.is_integral()) {
let (Some(from_nbits), Some(to_nbits)) = (
utils::int_ty_to_nbits(cx.tcx, cast_from),
utils::int_ty_to_nbits(cx.tcx, cast_to),
) else {
return;
}
};

// emit a lint if a cast is:
// 1. unsigned to signed
Expand All @@ -35,9 +38,6 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, ca
return;
}

let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);

let should_lint = match (cast_from.is_ptr_sized_integral(), cast_to.is_ptr_sized_integral()) {
(true, true) => {
// casts between two ptr sized integers are trivially always the same size
Expand Down
16 changes: 8 additions & 8 deletions src/tools/clippy/clippy_lints/src/casts/cast_precision_loss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use rustc_middle::ty::{self, FloatTy, Ty};
use super::{CAST_PRECISION_LOSS, utils};

pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
if !cast_from.is_integral() || cast_to.is_integral() {
let Some(from_nbits) = utils::int_ty_to_nbits(cx.tcx, cast_from) else {
return;
}
};

let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
let to_nbits = if cast_to.kind() == &ty::Float(FloatTy::F32) {
32
} else {
64
// FIXME: handle `f16` and `f128`
let to_nbits = match cast_to.kind() {
ty::Float(f @ (FloatTy::F32 | FloatTy::F64)) => f.bit_width(),
_ => return,
};

if !(is_isize_or_usize(cast_from) || from_nbits >= to_nbits) {
Expand All @@ -29,9 +28,10 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, ca
let from_nbits_str = if arch_dependent {
"64".to_owned()
} else if is_isize_or_usize(cast_from) {
// FIXME: handle 16 bits `usize` type
"32 or 64".to_owned()
} else {
utils::int_ty_to_nbits(cast_from, cx.tcx).to_string()
from_nbits.to_string()
};

span_lint(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn is_used_as_unaligned(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
};
match parent.kind {
ExprKind::MethodCall(name, self_arg, ..) if self_arg.hir_id == e.hir_id => {
if matches!(name.ident.as_str(), "read_unaligned" | "write_unaligned")
if matches!(name.ident.name, sym::read_unaligned | sym::write_unaligned)
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(parent.hir_id)
&& let Some(def_id) = cx.tcx.impl_of_method(def_id)
&& cx.tcx.type_of(def_id).instantiate_identity().is_raw_ptr()
Expand Down
14 changes: 6 additions & 8 deletions src/tools/clippy/clippy_lints/src/casts/fn_to_numeric_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ use clippy_utils::source::snippet_with_applicability;
use rustc_errors::Applicability;
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty, UintTy};
use rustc_middle::ty::{self, Ty};

use super::{FN_TO_NUMERIC_CAST, utils};

pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
// We only want to check casts to `ty::Uint` or `ty::Int`
match cast_to.kind() {
ty::Uint(_) | ty::Int(..) => { /* continue on */ },
_ => return,
}
let Some(to_nbits) = utils::int_ty_to_nbits(cx.tcx, cast_to) else {
return;
};

match cast_from.kind() {
ty::FnDef(..) | ty::FnPtr(..) => {
let mut applicability = Applicability::MaybeIncorrect;
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability);
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);

if (to_nbits >= cx.tcx.data_layout.pointer_size.bits()) && (*cast_to.kind() != ty::Uint(UintTy::Usize)) {
if to_nbits >= cx.tcx.data_layout.pointer_size.bits() && !cast_to.is_usize() {
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability);
span_lint_and_sugg(
cx,
FN_TO_NUMERIC_CAST,
Expand Down
Loading
Loading