diff options
| author | bors <bors@rust-lang.org> | 2022-10-16 03:11:06 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-10-16 03:11:06 +0000 |
| commit | ff33d6e712ebf1068e8d6109e56f2f5d1e82054c (patch) | |
| tree | 71b125d0e5f1fc27fbe9c17fa56e9e1d5fbe4fd6 | |
| parent | 50f192f86a12d4b39d8f7f061fbd8e0ba8007932 (diff) | |
| parent | f8ae2f580774c0d7743c5c6adf8e246735a28c92 (diff) | |
| download | rust-ff33d6e712ebf1068e8d6109e56f2f5d1e82054c.tar.gz rust-ff33d6e712ebf1068e8d6109e56f2f5d1e82054c.zip | |
Auto merge of #9655 - llogiq:unbox-default, r=dswij
fix `box-default` linting `no_std` non-boxes This fixes #9653 by doing the check against the `Box` type correctly even if `Box` isn't there, as in `no_std` code. Thanks to `@lukas-code` for opening the issue and supplying a reproducer! --- changelog: none
| -rw-r--r-- | clippy_lints/src/box_default.rs | 2 | ||||
| -rw-r--r-- | tests/ui/box_default_no_std.rs | 33 |
2 files changed, 34 insertions, 1 deletions
diff --git a/clippy_lints/src/box_default.rs b/clippy_lints/src/box_default.rs index f35a79dcc73..bb0307e8856 100644 --- a/clippy_lints/src/box_default.rs +++ b/clippy_lints/src/box_default.rs @@ -46,7 +46,7 @@ impl LateLintPass<'_> for BoxDefault { && !in_external_macro(cx.sess(), expr.span) && (expr.span.eq_ctxt(arg.span) || is_vec_expn(cx, arg)) && seg.ident.name == sym::new - && path_def_id(cx, ty) == cx.tcx.lang_items().owned_box() + && path_def_id(cx, ty).map_or(false, |id| Some(id) == cx.tcx.lang_items().owned_box()) && is_default_equivalent(cx, arg) { let arg_ty = cx.typeck_results().expr_ty(arg); diff --git a/tests/ui/box_default_no_std.rs b/tests/ui/box_default_no_std.rs new file mode 100644 index 00000000000..4326abc9a54 --- /dev/null +++ b/tests/ui/box_default_no_std.rs @@ -0,0 +1,33 @@ +#![feature(lang_items, start, libc)] +#![warn(clippy::box_default)] +#![no_std] + +pub struct NotBox<T> { + _value: T, +} + +impl<T> NotBox<T> { + pub fn new(value: T) -> Self { + Self { _value: value } + } +} + +impl<T: Default> Default for NotBox<T> { + fn default() -> Self { + Self::new(T::default()) + } +} + +#[start] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + let _p = NotBox::new(isize::default()); + 0 +} + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop {} +} + +#[lang = "eh_personality"] +extern "C" fn eh_personality() {} |
