Skip to content

Commit b3d41bf

Browse files
committed
Address review feedback
* Fix typo * Handle None value instead of using `unwrap()` * `pop()` instead of `x.truncate(x.len() - 1)`
1 parent 14ae53e commit b3d41bf

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

clippy_lints/src/explicit_write.rs

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::rustc::hir::*;
1212
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
1313
use crate::rustc::{declare_tool_lint, lint_array};
1414
use if_chain::if_chain;
15-
use crate::utils::{is_expn_of, match_def_path, resolve_node, span_lint_and_sugg};
15+
use crate::utils::{is_expn_of, match_def_path, resolve_node, span_lint, span_lint_and_sugg};
1616
use crate::utils::opt_def_id;
1717
use crate::syntax::ast::LitKind;
1818

@@ -86,35 +86,61 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
8686
};
8787

8888
// We need to remove the last trailing newline from the string because the
89-
// underlying `fmt::write` function doesn't know wether `println!` or `print!` was
89+
// underlying `fmt::write` function doesn't know whether `println!` or `print!` was
9090
// used.
91-
let mut write_output: String = write_output_string(write_args).unwrap();
92-
if write_output.ends_with('\n') {
93-
write_output.truncate(write_output.len() - 1)
94-
}
95-
if let Some(macro_name) = calling_macro {
96-
span_lint_and_sugg(
97-
cx,
98-
EXPLICIT_WRITE,
99-
expr.span,
100-
&format!(
101-
"use of `{}!({}(), ...).unwrap()`",
102-
macro_name,
103-
dest_name
104-
),
105-
"try this",
106-
format!("{}{}!(\"{}\")", prefix, macro_name.replace("write", "print"), write_output.escape_default())
107-
);
91+
if let Some(mut write_output) = write_output_string(write_args) {
92+
if write_output.ends_with('\n') {
93+
write_output.pop();
94+
}
95+
96+
if let Some(macro_name) = calling_macro {
97+
span_lint_and_sugg(
98+
cx,
99+
EXPLICIT_WRITE,
100+
expr.span,
101+
&format!(
102+
"use of `{}!({}(), ...).unwrap()`",
103+
macro_name,
104+
dest_name
105+
),
106+
"try this",
107+
format!("{}{}!(\"{}\")", prefix, macro_name.replace("write", "print"), write_output.escape_default())
108+
);
109+
} else {
110+
span_lint_and_sugg(
111+
cx,
112+
EXPLICIT_WRITE,
113+
expr.span,
114+
&format!("use of `{}().write_fmt(...).unwrap()`", dest_name),
115+
"try this",
116+
format!("{}print!(\"{}\")", prefix, write_output.escape_default())
117+
);
118+
}
108119
} else {
109-
span_lint_and_sugg(
110-
cx,
111-
EXPLICIT_WRITE,
112-
expr.span,
113-
&format!("use of `{}().write_fmt(...).unwrap()`", dest_name),
114-
"try this",
115-
format!("{}print!(\"{}\")", prefix, write_output.escape_default())
116-
);
120+
// We don't have a proper suggestion
121+
if let Some(macro_name) = calling_macro {
122+
span_lint(
123+
cx,
124+
EXPLICIT_WRITE,
125+
expr.span,
126+
&format!(
127+
"use of `{}!({}(), ...).unwrap()`. Consider using `{}{}!` instead",
128+
macro_name,
129+
dest_name,
130+
prefix,
131+
macro_name.replace("write", "print")
132+
)
133+
);
134+
} else {
135+
span_lint(
136+
cx,
137+
EXPLICIT_WRITE,
138+
expr.span,
139+
&format!("use of `{}().write_fmt(...).unwrap()`. Consider using `{}print!` instead", dest_name, prefix),
140+
);
141+
}
117142
}
143+
118144
}
119145
}
120146
}

0 commit comments

Comments
 (0)