Skip to content

Commit bc62c10

Browse files
committed
remove the format_string_expr from the FormatArgsExpn
1 parent cd7027c commit bc62c10

File tree

4 files changed

+75
-35
lines changed

4 files changed

+75
-35
lines changed

clippy_lints/src/bool_assert_comparison.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
8181
.chain(inverted_macros.iter().map(|el| (el, false)))
8282
{
8383
if let Some(span) = is_direct_expn_of(expr.span, mac) {
84-
if let Some(args) = AssertExpn::parse(expr).map(|v| v.argument_vector()) {
85-
if let [a, b, ref fmt_args @ ..] = args[..] {
84+
if let Some(parse_assert) = AssertExpn::parse(expr) {
85+
if let [a, b] = parse_assert.assert_arguments()[..] {
8686
let (lit_value, other_expr) = match (bool_lit(a), bool_lit(b)) {
8787
(Some(lit), None) => (lit, b),
8888
(None, Some(lit)) => (lit, a),
@@ -110,23 +110,14 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
110110
} else {
111111
format!("{}", sugg.maybe_par())
112112
};
113-
114-
let arg_span = match fmt_args {
113+
let fmt_args = parse_assert.format_arguments(cx, &mut applicability);
114+
let arg_span = match &fmt_args[..] {
115115
[] => None,
116-
[a] => Some(format!(
117-
"{}",
118-
Sugg::hir_with_applicability(cx, a, "..", &mut applicability)
119-
)),
116+
[a] => Some(a.to_string()),
120117
_ => {
121-
let mut args = format!(
122-
"{}",
123-
Sugg::hir_with_applicability(cx, fmt_args[0], "..", &mut applicability)
124-
);
118+
let mut args = fmt_args[0].to_string();
125119
for el in &fmt_args[1..] {
126-
args.push_str(&format!(
127-
", {}",
128-
Sugg::hir_with_applicability(cx, el, "..", &mut applicability)
129-
));
120+
args.push_str(&format!(", {}", el));
130121
}
131122
Some(args)
132123
},

clippy_lints/src/eq_op.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'tcx> LateLintPass<'tcx> for EqOp {
7777
if_chain! {
7878
if is_expn_of(stmt.span, amn).is_some();
7979
if let StmtKind::Semi(matchexpr) = stmt.kind;
80-
if let Some(macro_args) = AssertExpn::parse(matchexpr).map(|v| v.argument_vector());
80+
if let Some(macro_args) = AssertExpn::parse(matchexpr).map(|v| v.assert_arguments());
8181
if macro_args.len() == 2;
8282
let (lhs, rhs) = (macro_args[0], macro_args[1]);
8383
if eq_expr_value(cx, lhs, rhs);

clippy_lints/src/mutable_debug_assertion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<'tcx> LateLintPass<'tcx> for DebugAssertWithMutCall {
3939
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
4040
for dmn in &DEBUG_MACRO_NAMES {
4141
if is_direct_expn_of(e.span, dmn).is_some() {
42-
if let Some(macro_args) = AssertExpn::parse(e).map(|v| v.argument_vector()) {
42+
if let Some(macro_args) = AssertExpn::parse(e).map(|v| v.assert_arguments()) {
4343
for arg in macro_args {
4444
let mut visitor = MutArgVisitor::new(cx);
4545
visitor.visit_expr(arg);

clippy_utils/src/higher.rs

+66-17
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
33
#![deny(clippy::missing_docs_in_private_items)]
44

5-
use crate::{is_expn_of, match_def_path, paths};
5+
use crate::{is_expn_of, match_def_path, paths, source::snippet_with_applicability};
66
use if_chain::if_chain;
77
use rustc_ast::ast::{self, LitKind};
8+
use rustc_errors::Applicability;
89
use rustc_hir as hir;
910
use rustc_hir::{Arm, Block, BorrowKind, Expr, ExprKind, LoopSource, MatchSource, Node, Pat, StmtKind, UnOp};
1011
use rustc_lint::LateContext;
1112
use rustc_span::{sym, ExpnKind, Span, Symbol};
1213

14+
use std::borrow::Cow;
15+
1316
/// The essential nodes of a desugared for loop as well as the entire span:
1417
/// `for pat in arg { body }` becomes `(pat, arg, body)`. Return `(pat, arg, body, span)`.
1518
pub struct ForLoop<'tcx> {
@@ -446,14 +449,33 @@ impl<'tcx> AssertExpn<'tcx> {
446449
if let StmtKind::Semi(matchexpr) = block.stmts.get(0)?.kind {
447450
// macros with unique arg: `{debug_}assert!` (e.g., `debug_assert!(some_condition)`)
448451
if_chain! {
449-
if let Some(If { cond, .. }) = If::hir(matchexpr);
452+
if let Some(If { cond, then, .. }) = If::hir(matchexpr);
450453
if let ExprKind::Unary(UnOp::Not, condition) = cond.kind;
451454
then {
452-
return Some(Self {
453-
first_assert_argument: condition,
454-
second_assert_argument: None,
455-
format_arg: None, // FIXME actually parse the aguments
456-
});
455+
if_chain! {
456+
if let ExprKind::Block(block, _) = then.kind;
457+
if let [statement, ..] = block.stmts;
458+
if let StmtKind::Expr(call_assert_failed)
459+
| StmtKind::Semi(call_assert_failed) = statement.kind;
460+
if let ExprKind::Call(_, args_assert_failed) = call_assert_failed.kind;
461+
if args_assert_failed.len() >= 1;
462+
if let ExprKind::Call(_, [arg, ..]) = args_assert_failed[0].kind;
463+
if let Some(format_arg_expn) = FormatArgsExpn::parse(&arg);
464+
then {
465+
return Some(Self {
466+
first_assert_argument: condition,
467+
second_assert_argument: None,
468+
format_arg: Some(format_arg_expn), // FIXME actually parse the aguments
469+
});
470+
}
471+
else{
472+
return Some(Self {
473+
first_assert_argument: condition,
474+
second_assert_argument: None,
475+
format_arg: None,
476+
});
477+
}
478+
}
457479
}
458480
}
459481

@@ -516,19 +538,49 @@ impl<'tcx> AssertExpn<'tcx> {
516538
None
517539
}
518540

519-
/// Gives the argument as a vector
520-
pub fn argument_vector(&self) -> Vec<&'tcx Expr<'tcx>> {
541+
/// Gives the argument in the comparaison as a vector leaving the format
542+
pub fn assert_arguments(&self) -> Vec<&'tcx Expr<'tcx>> {
521543
let mut expr_vec = vec![self.first_assert_argument];
522544
if let Some(sec_agr) = self.second_assert_argument {
523545
expr_vec.push(sec_agr);
524546
}
525-
if let Some(ref format_arg) = self.format_arg {
526-
expr_vec.push(format_arg.format_string);
527-
for arg in &format_arg.value_args {
528-
expr_vec.push(arg)
547+
expr_vec
548+
}
549+
550+
/// Gives the argument passed to the macro as a string
551+
pub fn all_arguments_string(
552+
&self,
553+
cx: &LateContext<'_>,
554+
applicability: &mut Applicability,
555+
) -> Vec<Cow<'static, str>> {
556+
let mut vec_arg = vec![snippet_with_applicability(
557+
cx,
558+
self.first_assert_argument.span,
559+
"..",
560+
applicability,
561+
)];
562+
if let Some(sec_agr) = self.second_assert_argument {
563+
vec_arg.push(snippet_with_applicability(cx, sec_agr.span, "..", applicability));
564+
}
565+
vec_arg.append(&mut self.format_arguments(cx, applicability));
566+
vec_arg
567+
}
568+
569+
/// Returns only the format agruments
570+
pub fn format_arguments(&self, cx: &LateContext<'_>, applicability: &mut Applicability) -> Vec<Cow<'static, str>> {
571+
let mut vec_arg = vec![];
572+
if let Some(ref fmt_arg) = self.format_arg {
573+
vec_arg.push(snippet_with_applicability(
574+
cx,
575+
fmt_arg.format_string_span,
576+
"..",
577+
applicability,
578+
));
579+
for arg in &fmt_arg.value_args {
580+
vec_arg.push(snippet_with_applicability(cx, arg.span, "..", applicability));
529581
}
530582
}
531-
expr_vec
583+
vec_arg
532584
}
533585
}
534586

@@ -566,8 +618,6 @@ impl FormatExpn<'tcx> {
566618

567619
/// A parsed `format_args!` expansion
568620
pub struct FormatArgsExpn<'tcx> {
569-
/// The fist argument, the fromat string, as an expr
570-
pub format_string: &'tcx Expr<'tcx>,
571621
/// Span of the first argument, the format string
572622
pub format_string_span: Span,
573623
/// Values passed after the format string
@@ -629,7 +679,6 @@ impl FormatArgsExpn<'tcx> {
629679
.collect();
630680
then {
631681
Some(FormatArgsExpn {
632-
format_string:strs_ref,
633682
format_string_span: strs_ref.span,
634683
value_args,
635684
format_string_parts,

0 commit comments

Comments
 (0)