2 use rustc::hir::map::Node::{NodeItem, NodeImplItem};
5 use utils::{is_expn_of, match_def_path, resolve_node, span_lint, match_path_old};
6 use format::get_argument_fmtstr_parts;
8 /// **What it does:** This lint warns when you using `print!()` with a format
10 /// ends in a newline.
12 /// **Why is this bad?** You should use `println!()` instead, which appends the
15 /// **Known problems:** None.
19 /// print!("Hello {}!\n", name);
22 pub PRINT_WITH_NEWLINE,
24 "using `print!()` with a format string that ends in a newline"
27 /// **What it does:** Checks for printing on *stdout*. The purpose of this lint
28 /// is to catch debugging remnants.
30 /// **Why is this bad?** People often print on *stdout* while debugging an
31 /// application and might forget to remove those prints afterward.
33 /// **Known problems:** Only catches `print!` and `println!` calls.
37 /// println!("Hello world!");
45 /// **What it does:** Checks for use of `Debug` formatting. The purpose of this
46 /// lint is to catch debugging remnants.
48 /// **Why is this bad?** The purpose of the `Debug` trait is to facilitate
49 /// debugging Rust code. It should not be used in in user-facing output.
53 /// println!("{:?}", foo);
58 "use of `Debug`-based formatting"
61 #[derive(Copy, Clone, Debug)]
64 impl LintPass for Pass {
65 fn get_lints(&self) -> LintArray {
66 lint_array!(PRINT_WITH_NEWLINE, PRINT_STDOUT, USE_DEBUG)
70 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
71 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
73 let ExprCall(ref fun, ref args) = expr.node,
74 let ExprPath(ref qpath) = fun.node,
76 let fun = resolve_node(cx, qpath, fun.hir_id);
77 let fun_id = fun.def_id();
79 // Search for `std::io::_print(..)` which is unique in a
80 // `print!` expansion.
81 if match_def_path(cx.tcx, fun_id, &paths::IO_PRINT) {
82 if let Some(span) = is_expn_of(expr.span, "print") {
83 // `println!` uses `print!`.
84 let (span, name) = match is_expn_of(span, "println") {
85 Some(span) => (span, "println"),
86 None => (span, "print"),
89 span_lint(cx, PRINT_STDOUT, span, &format!("use of `{}!`", name));
91 // Check print! with format string ending in "\n".
95 // ensure we're calling Arguments::new_v1
97 let ExprCall(ref args_fun, ref args_args) = args[0].node,
98 let ExprPath(ref qpath) = args_fun.node,
99 match_def_path(cx.tcx,
100 resolve_node(cx, qpath, args_fun.hir_id).def_id(),
101 &paths::FMT_ARGUMENTS_NEWV1),
102 args_args.len() == 2,
103 let ExprAddrOf(_, ref match_expr) = args_args[1].node,
104 let ExprMatch(ref args, _, _) = match_expr.node,
105 let ExprTup(ref args) = args.node,
107 // collect the format string parts and check the last one
108 let Some(fmtstrs) = get_argument_fmtstr_parts(cx, &args_args[0]),
109 let Some(last_str) = fmtstrs.last(),
110 let Some('\n') = last_str.chars().last(),
112 // "foo{}bar" is made into two strings + one argument,
113 // if the format string starts with `{}` (eg. "{}foo"),
114 // the string array is prepended an empty string "".
115 // We only want to check the last string after any `{}`:
116 args.len() < fmtstrs.len(),
118 span_lint(cx, PRINT_WITH_NEWLINE, span,
119 "using `print!()` with a format string that ends in a \
120 newline, consider using `println!()` instead");
124 // Search for something like
125 // `::std::fmt::ArgumentV1::new(__arg0, ::std::fmt::Debug::fmt)`
126 else if args.len() == 2 && match_def_path(cx.tcx, fun_id, &paths::FMT_ARGUMENTV1_NEW) {
127 if let ExprPath(ref qpath) = args[1].node {
128 let def_id = cx.tables.qpath_def(qpath, args[1].hir_id).def_id();
129 if match_def_path(cx.tcx, def_id, &paths::DEBUG_FMT_METHOD) && !is_in_debug_impl(cx, expr) &&
130 is_expn_of(expr.span, "panic").is_none() {
131 span_lint(cx, USE_DEBUG, args[0].span, "use of `Debug`-based formatting");
139 fn is_in_debug_impl(cx: &LateContext, expr: &Expr) -> bool {
140 let map = &cx.tcx.hir;
143 if let Some(NodeImplItem(item)) = map.find(map.get_parent(expr.id)) {
145 if let Some(NodeItem(item)) = map.find(map.get_parent(item.id)) {
146 if let ItemImpl(_, _, _, _, Some(ref tr), _, _) = item.node {
147 return match_path_old(&tr.path, &["Debug"]);