Skip to content

Commit e553105

Browse files
committed
Extract method
1 parent e4e4393 commit e553105

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

clippy_lints/src/explicit_write.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
5151
if unwrap_args.len() > 0;
5252
if let ExprKind::MethodCall(ref write_fun, _, ref write_args) =
5353
unwrap_args[0].node;
54-
// Obtain the string that should be printed
55-
if write_args.len() > 1;
56-
if let ExprKind::Call(_, ref output_args) = write_args[1].node;
57-
if output_args.len() > 0;
58-
if let ExprKind::AddrOf(_, ref output_string_expr) = output_args[0].node;
59-
if let ExprKind::Array(ref string_exprs) = output_string_expr.node;
60-
if string_exprs.len() > 0;
61-
if let ExprKind::Lit(ref lit) = string_exprs[0].node;
62-
if let LitKind::Str(ref write_output, _) = lit.node;
6354
if write_fun.ident.name == "write_fmt";
6455
// match calls to std::io::stdout() / std::io::stderr ()
6556
if write_args.len() > 0;
@@ -94,7 +85,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
9485
// We need to remove the last trailing newline from the string because the
9586
// underlying `fmt::write` function doesn't know wether `println!` or `print!` was
9687
// used.
97-
let mut write_output: String = write_output.to_string();
88+
let mut write_output: String = write_output_string(write_args).unwrap();
9889
if write_output.ends_with('\n') {
9990
write_output.truncate(write_output.len() - 1)
10091
}
@@ -125,3 +116,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
125116
}
126117
}
127118
}
119+
120+
// Extract the output string from the given `write_args`.
121+
fn write_output_string(write_args: &HirVec<Expr>) -> Option<String> {
122+
if_chain! {
123+
// Obtain the string that should be printed
124+
if write_args.len() > 1;
125+
if let ExprKind::Call(_, ref output_args) = write_args[1].node;
126+
if output_args.len() > 0;
127+
if let ExprKind::AddrOf(_, ref output_string_expr) = output_args[0].node;
128+
if let ExprKind::Array(ref string_exprs) = output_string_expr.node;
129+
if string_exprs.len() > 0;
130+
if let ExprKind::Lit(ref lit) = string_exprs[0].node;
131+
if let LitKind::Str(ref write_output, _) = lit.node;
132+
then {
133+
return Some(write_output.to_string())
134+
}
135+
}
136+
None
137+
}

0 commit comments

Comments
 (0)