Extract method

This commit is contained in:
Philipp Hansch 2018-11-24 12:17:43 +01:00
parent 7e7a33c726
commit 5f007a88b4
No known key found for this signature in database
GPG Key ID: B6FA06A6E0E2665B

View File

@ -51,15 +51,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if unwrap_args.len() > 0;
if let ExprKind::MethodCall(ref write_fun, _, ref write_args) =
unwrap_args[0].node;
// Obtain the string that should be printed
if write_args.len() > 1;
if let ExprKind::Call(_, ref output_args) = write_args[1].node;
if output_args.len() > 0;
if let ExprKind::AddrOf(_, ref output_string_expr) = output_args[0].node;
if let ExprKind::Array(ref string_exprs) = output_string_expr.node;
if string_exprs.len() > 0;
if let ExprKind::Lit(ref lit) = string_exprs[0].node;
if let LitKind::Str(ref write_output, _) = lit.node;
if write_fun.ident.name == "write_fmt";
// match calls to std::io::stdout() / std::io::stderr ()
if write_args.len() > 0;
@ -94,7 +85,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
// We need to remove the last trailing newline from the string because the
// underlying `fmt::write` function doesn't know wether `println!` or `print!` was
// used.
let mut write_output: String = write_output.to_string();
let mut write_output: String = write_output_string(write_args).unwrap();
if write_output.ends_with('\n') {
write_output.truncate(write_output.len() - 1)
}
@ -125,3 +116,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
}
}
}
// Extract the output string from the given `write_args`.
fn write_output_string(write_args: &HirVec<Expr>) -> Option<String> {
if_chain! {
// Obtain the string that should be printed
if write_args.len() > 1;
if let ExprKind::Call(_, ref output_args) = write_args[1].node;
if output_args.len() > 0;
if let ExprKind::AddrOf(_, ref output_string_expr) = output_args[0].node;
if let ExprKind::Array(ref string_exprs) = output_string_expr.node;
if string_exprs.len() > 0;
if let ExprKind::Lit(ref lit) = string_exprs[0].node;
if let LitKind::Str(ref write_output, _) = lit.node;
then {
return Some(write_output.to_string())
}
}
None
}