-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[bool_assert_comparaison
] improve suggestion
#7612
[bool_assert_comparaison
] improve suggestion
#7612
Conversation
r? @camsteffen (rust-highfive has picked a reviewer for you, use r? to override) |
bcfe69d
to
522e3c7
Compare
for el in &args[2..] { | ||
str = format!("{}, {}", str, source::snippet(cx, el.span, "")); | ||
} | ||
format!("{})", str) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the String
API (push
and push_str
).
Or better yet, get just one snippet of all the format args with something like
match fmt_args {
[] => None,
[a] => Some(a.span),
[a, .., b] => Some(a.span.to(b.span)),
}
The problem I see is that |
You can open a Rust Playground with an |
Yes it does thank you. |
Hi @ABouttefeux, it looks like tests are not passing. Also please do a rebase instead of a merge since we follow rust's no-merge policy: https://rustc-dev-guide.rust-lang.org/git.html#no-merge-policy |
e44bb26
to
f244ec7
Compare
I am not able yet to extract the fist formatting argument, I change it to ".." temporarily. I will work on it later. Do you have any idea how to extract it ? |
That would come from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work getting the macro stuff working! Just some feedback with how the utils are structured and we should enable rustfix for tests now that we have valid suggestions.
@@ -90,13 +101,46 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison { | |||
} | |||
|
|||
let non_eq_mac = &mac[..mac.len() - 3]; | |||
let mut applicability = Applicability::MaybeIncorrect; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use MachineApplicable here. Also add // run-rustfix
to the top of the test file.
clippy_utils/src/higher.rs
Outdated
| StmtKind::Semi(call_assert_failed) = stmts_if_block[1].kind; | ||
if let ExprKind::Call(_, args_assert_failed) = call_assert_failed.kind; | ||
if args_assert_failed.len() >= 4; | ||
if let ExprKind::Call(_, args) = args_assert_failed[3].kind; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer destructuring for stmts, args, etc.
if let ExprKind::Call(_, args) = args_assert_failed[3].kind; | |
if let ExprKind::Call(_, [arg]) = args_assert_failed[3].kind; |
clippy_utils/src/higher.rs
Outdated
if let Some (mut format_arg_expn) = FormatArgsExpn::parse(&args[0]); | ||
then { | ||
vec_arg.push(format_arg_expn.format_string); | ||
vec_arg.append(&mut format_arg_expn.value_args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should change extract_assert_macro_args
to be more consistent with other utils here. We could have AssertExpn::parse
and AssertExpn
is a struct that contains a FormatArgsExpn
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks I have done something similar, but now I would like to test parsing assert(expr, "format {}", value), how can I test that ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Expand macros in the rust playground to see what you are trying to parse. For other tips, use dbg!()
to set a checkpoint in your code and see if that line is being reached. Use dbg!(expr)
to see the HIR. Use let _ = dbg!();
in if_chain!
s.
bc62c10
to
29fc388
Compare
clippy_utils/src/higher.rs
Outdated
second_assert_argument: None, | ||
format_arg: None, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to avoid else
in if_chain!
here since it adds an else clause to every if
in the expansion. You should only need to "fork" at a single point.
/// `Some(AssertExpn { first_assert_argument: a, second_assert_argument: Some(b), | ||
/// format_arg:None })` | ||
pub fn parse(e: &'tcx Expr<'tcx>) -> Option<Self> { | ||
if let ExprKind::Block(block, _) = e.kind { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider doing something like this as a first step
if let ExpnKind::Macro(_, name) = expr.span.ctxt().outer_expn_data().kind;
match *name.as_str() {
"assert" | "debug_assert" => ..,
"assert_eq" | "debug_assert_eq" => ..,
}
ping from triage @ABouttefeux. It looks like there are a few things left to fix. Can you address these? |
☔ The latest upstream changes (presumably #7682) made this pull request unmergeable. Please resolve the merge conflicts. |
I have little time I will try to do it. Actually I am strugling a bit with |
d006faa
to
5c0488f
Compare
☔ The latest upstream changes (presumably #7743) made this pull request unmergeable. Please resolve the merge conflicts. |
5c0488f
to
add7d76
Compare
☔ The latest upstream changes (presumably #7811) made this pull request unmergeable. Please resolve the merge conflicts. |
add7d76
to
929610a
Compare
☔ The latest upstream changes (presumably #7853) made this pull request unmergeable. Please resolve the merge conflicts. |
929610a
to
3328501
Compare
Hey @ABouttefeux, let me know when you are ready for review. Also I want to point out #7843 - we may need to take a different direction for parsing the assert macros that does not parse the exact structure of the expansion. Although this is somewhat pre-existing so maybe it's okay to continue with the current approach for this PR. (@rust-lang/clippy thoughts?) I think it is possible to take a different approach using a visitor and checking span expansion data, but this is quite tricky. |
3328501
to
103ac52
Compare
Yes this is ready. I have no idea how do to in a way that would be more robust to change of the macro however. |
I'm really unsure if we should take up more technical debt here, since we know that this is not sustainable long term. This is a nice improvement of the suggestion, so definitely a welcome change. But I'm not sure if it is worth to take up the technical debt that comes with it. |
☔ The latest upstream changes (presumably #7906) made this pull request unmergeable. Please resolve the merge conflicts. |
@ABouttefeux After #8219 is merged, have another look if you're still interested. There are new utils in place for assert macros. |
ping from triage @ABouttefeux. #8219 was merged, and you should be able to use those new utils to implement this lint and get this PR merged. |
Closing due to inactivity. Feel free to reopen if you'd like to pick this up again. |
@rustbot label -S-inactive-closed |
close #7598
now code like
Will suggest to use
assert(a)
wherease previously it wasassert(..)
.Also what I would like to add is with
is to suggest
assert!( a, "message {}", b)
.It is not the case at the moment.
changelog: [
bool_assert_comparaison
] improve suggestion