Skip to content

Commit c84a17d

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 e553105 commit c84a17d

File tree

1 file changed

+52
-26
lines changed

1 file changed

+52
-26
lines changed

clippy_lints/src/explicit_write.rs

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,35 +83,61 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
8383
};
8484

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

0 commit comments

Comments
 (0)