Conversation
|
Sadly, I accidentally deleted the branch connected to my previous PR, causing it to close. @ada4a |
|
Lintcheck changes for c38698e
This comment will be updated if you push new changes |
Happens to the best of us, don't worry... Looking at Lintcheck, there seem to be some classes of cases where we don't really want to lint:
|
clippy_lints/src/assert_multiple.rs
Outdated
| // which is represented by `'v` (the HIR lifetime `'tcx` refers to the | ||
| // data inside the `LateContext`, not the borrow itself). | ||
| cx: &'v LateContext<'tcx>, | ||
| suggest_asserts: &'v mut Vec<String>, |
There was a problem hiding this comment.
at this point you might as well just own the vec -- would make lifetime management easier
I'm not sure I understand the comment about lintcheck. can you clarify please? This is based on issue 1810. |
|
Ah, sorry. Lintcheck is a thing where the PR is run on a bunch of popular crates, in order to see if the lint is doing the right thing, and catch false positives. You can see its results in #16677 (comment) by clicking on the |
clippy_lints/src/assert_multiple.rs
Outdated
| // the visitor needs a mutable reference to a vector that lives | ||
| // only for the duration of a single `check_expr` invocation. we | ||
| // therefore introduce a separate lifetime `'v` for that borrow. |
There was a problem hiding this comment.
the comment is outdated now, feel free to remove/rephrase
clippy_lints/src/assert_multiple.rs
Outdated
| impl<'tcx> LateLintPass<'tcx> for AssertMultiple { | ||
| fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) { | ||
| if let Some(macro_call) = root_macro_call_first_node(cx, e) | ||
| && matches!(cx.tcx.get_diagnostic_name(macro_call.def_id), Some(sym::assert_macro)) |
There was a problem hiding this comment.
This could be left as future work, but I don't think there's anything stopping this lint from also working with debug_assert!?
| self.suggests.push(tmptxt); | ||
| }, | ||
|
|
||
| _ => {}, |
There was a problem hiding this comment.
I think this will end up eating the subexpressions which don't match any of the kinds above? For example, given a: i32 and is_foo: bool, the following code:
assert!(a > 0 && is_foo);would become just
assert!(a > 0);Right?
|
in the changelog message, the lint name should be in backticks ( |
The wrong `TypeckResults` was used in the fallback equality function passed by the `match_same_arms` and `filter_map` lints. Previously, those fallback functions had no way of using the proper `TypeckResults`. Those (one per expression being compared) are now passed to the registered fallback function.
It is not necessary to materialize snippets into `String` objects just to check if they contain comments for example.
|
My latest update passes most tests, but the clippy test complains about collapsing my span_lint_and_then call into one (?) line. cargo dev fmt has a different idea. How does this get resolved? On the other front, it now supports debug_assert!() as well as assert!(). I also improved the handling of Or. |
Ah no, it also suggests switching to span_lint_and_sugg(cx, ASSERT_MULTIPLE, e.span, "multiple asserts combined into one", "consider writing", am_visitor.suggests.join("\n"), Applicability::MaybeIncorrect) |
| #[clippy::version = "1.95.0"] | ||
| pub ASSERT_MULTIPLE, | ||
| nursery, | ||
| "Splitting an assert using '&&' into separate asserts makes it clearer which is failing." |
There was a problem hiding this comment.
| "Splitting an assert using '&&' into separate asserts makes it clearer which is failing." | |
| "Splitting an assert using `&&` into separate asserts makes it clearer which is failing." |
| && matches!( | ||
| cx.tcx.get_diagnostic_name(macro_call.def_id), | ||
| Some(sym::assert_macro | sym::debug_assert_macro) | ||
| ) |
There was a problem hiding this comment.
to avoid matching on the diagnostic name again below, you can do something like this:
| && matches!( | |
| cx.tcx.get_diagnostic_name(macro_call.def_id), | |
| Some(sym::assert_macro | sym::debug_assert_macro) | |
| ) | |
| && let assert_string = match cx.tcx.get_diagnostic_name(macro_call.def_id) { | |
| Some(sym::assert_macro) => "assert", | |
| Some(sym::debug_assert_macro) => "debug_assert", | |
| _ => return, | |
| ) |
Note that this also uses &'static strs instead of owned Strings, since you don't really need ownership here.
| "multiple asserts combined into one", | ||
| |diag| { | ||
| diag.span_suggestion( | ||
| e.span, |
There was a problem hiding this comment.
ohhhhh, I think I finally know why you haven't been getting any suggestions! I think it's because e here is a macro call, so its .span might be pointing to the definition of assert!, which is why it doesn't get shown. Try replacing this with macro_call.span
|
Also, I'm not sure how you ended up putting a bunch of other people's commits onto your branch?.. Try |
Please write a short comment explaining your change (or "none" for internal only changes)
changelog: ['assert_multiple']: Add an assert for asserts with mulitple boolean tests