-
Notifications
You must be signed in to change notification settings - Fork 888
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
False negative with error_on_line_overflow
in Clippy source file
#5700
Comments
Thanks for reaching out. Confirming I can reproduce this with fn check_copy_clone<'tcx>(cx: &LateContext<'tcx>, item: &Item<'_>, trait_ref: &hir::TraitRef<'_>, ty: Ty<'tcx>) {
let clone_id = match cx.tcx.lang_items().clone_trait() {
Some(id) if trait_ref.trait_def_id() == Some(id) => id,
_ => return,
};
let Some(copy_id) = cx.tcx.lang_items().copy_trait() else { return };
let (ty_adt, ty_subs) = match *ty.kind() {
// Unions can't derive clone.
ty::Adt(adt, subs) if !adt.is_union() => (adt, subs),
_ => return,
};
// If the current self type doesn't implement Copy (due to generic constraints), search to see if
// there's a Copy impl for any instance of the adt.
if !is_copy(cx, ty) {
if ty_subs.non_erasable_generics().next().is_some() {
let has_copy_impl = cx.tcx.all_local_trait_impls(()).get(©_id).map_or(false, |impls| {
impls
.iter()
.any(|&id| matches!(cx.tcx.type_of(id).subst_identity().kind(), ty::Adt(adt, _) if ty_adt.did() == adt.did()))
});
if !has_copy_impl {
return;
}
} else {
return;
}
}
// Derive constrains all generic types to requiring Clone. Check if any type is not constrained for
// this impl.
if ty_subs.types().any(|ty| !implements_trait(cx, ty, clone_id, &[])) {
return;
}
// `#[repr(packed)]` structs with type/const parameters can't derive `Clone`.
// https://github.com/rust-lang/rust-clippy/issues/10188
if ty_adt.repr().packed()
&& ty_subs
.iter()
.any(|arg| matches!(arg.unpack(), GenericArgKind::Type(_) | GenericArgKind::Const(_)))
{
return;
}
span_lint_and_note(
cx,
EXPL_IMPL_CLONE_ON_COPY,
item.span,
"you are implementing `Clone` explicitly on a `Copy` type",
Some(item.span),
"consider deriving `Clone` or removing `Copy`",
);
} What's interesting is that with
|
linking tracking issue for |
I did some digging and I've figured out why we're not emitting a The short answer is we're failing to emit an error because rustfmt doesn't currently support formatting Failing to format the macro leads to rustfmt adding the macro to the Lines 138 to 141 in 34f9ca2
which is behavior that was added by PR #3768. Then when rustfmt gets to the point where it determines if it should emit a Lines 551 to 560 in 34f9ca2
It would seem that PR #3768 extended the use case for Lines 83 to 85 in 34f9ca2
So why do we emit an error message when It turns out that when we add the In both the This offsets the |
Wow. That you put so much work into this in such a short period of time is, frankly, awe inspiriing, Thanks tremendously, @ytmimi! |
@smoelius thanks for the kind words 😁 It definitely helps that I knew where to start looking. Now that we understand the underlying issue we can try to come up with a solution. It may be as simple as removing the lines in return_macro_parse_failure_fallback, but more work will be needed to understand how removing those lines affect macro formatting failures. We may have to come up with a completely different solution. Unfortunately I don't have the bandwidth to do a deep dive into this right now, but I'm happy to help anyone who wants to take this on. |
I would have expected you to say: changing that code to insert the line range after formatting. Is that not desired? Could you briefly explain why? |
@smoelius Just to be clear I was suggesting we try to remove these lines of code altogether: Lines 138 to 141 in 34f9ca2
The false positive that you've pointed out in this issue is directly caused by rustfmt inserting line ranges into the |
Thank you, @ytmimi. Your explanation makes sense to me. |
I'd like to request that we hold off on changing rustfmt behavior for the moment as I haven't had the bandwidth to process this thread yet. Salient tl;dr, however, is that rustfmt intentionally handles macros differently and it's correct/intentional/desirable behavior that rustfmt does not error in cases where an unformattable macro exceeds width boundaries. We can certainly discuss whether we want to change that, but I don't think that's something we'll be able to thoroughly consider any time in the near future |
@calebcartwright if the current behavior is correct, then we've got a separate issue to deal with that I highlight in the second half of #5700 (comment) where I answer the question |
The issue concerns this line:
https://github.com/rust-lang/rust-clippy/blob/98c4a49db8c305c468646715b773fa9b6ec7049a/clippy_lints/src/derive.rs#L350
The line is 130 characters long, which exceeds the
max_width
declared in therustfmt.toml
file:https://github.com/rust-lang/rust-clippy/blob/98c4a49db8c305c468646715b773fa9b6ec7049a/rustfmt.toml#L1
Yet none of these commands produce a change or error:
(Re
+nightly
, recall Clippy uses arust-toolchain
file.)The text was updated successfully, but these errors were encountered: