Skip to content

Commit 5c7278a

Browse files
committed
return when expr has error
fmt add a comment
1 parent c0e9c86 commit 5c7278a

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

compiler/rustc_lint/src/unused.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -636,13 +636,34 @@ trait UnusedDelimLint {
636636
left_pos: Option<BytePos>,
637637
right_pos: Option<BytePos>,
638638
) {
639+
// If `value` has `ExprKind::Err`, unused delim lint can be broken.
640+
// For example, the following code caused ICE.
641+
// This is because the `ExprKind::Call` in `value` has `ExprKind::Err` as its argument
642+
// and this leads to wrong spans. #104897
643+
//
644+
// ```
645+
// fn f(){(print!(á
646+
// ```
647+
use rustc_ast::visit::{walk_expr, Visitor};
648+
struct ErrExprVisitor {
649+
has_error: bool,
650+
}
651+
impl<'ast> Visitor<'ast> for ErrExprVisitor {
652+
fn visit_expr(&mut self, expr: &'ast ast::Expr) {
653+
if let ExprKind::Err = expr.kind {
654+
self.has_error = true;
655+
return;
656+
}
657+
walk_expr(self, expr)
658+
}
659+
}
660+
let mut visitor = ErrExprVisitor { has_error: false };
661+
visitor.visit_expr(value);
662+
if visitor.has_error {
663+
return;
664+
}
639665
let spans = match value.kind {
640666
ast::ExprKind::Block(ref block, None) if block.stmts.len() == 1 => {
641-
if let StmtKind::Expr(expr) = &block.stmts[0].kind
642-
&& let ExprKind::Err = expr.kind
643-
{
644-
return
645-
}
646667
if let Some(span) = block.stmts[0].span.find_ancestor_inside(value.span) {
647668
Some((value.span.with_hi(span.lo()), value.span.with_lo(span.hi())))
648669
} else {

src/test/ui/lint/issue-104897.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// error-pattern: this file contains an unclosed delimiter
2+
// error-pattern: this file contains an unclosed delimiter
3+
// error-pattern: this file contains an unclosed delimiter
4+
// error-pattern: format argument must be a string literal
5+
6+
fn f(){(print!(á

src/test/ui/lint/issue-104897.stderr

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error: this file contains an unclosed delimiter
2+
--> $DIR/issue-104897.rs:6:18
3+
|
4+
LL | fn f(){(print!(á
5+
| -- - ^
6+
| || |
7+
| || unclosed delimiter
8+
| |unclosed delimiter
9+
| unclosed delimiter
10+
11+
error: this file contains an unclosed delimiter
12+
--> $DIR/issue-104897.rs:6:18
13+
|
14+
LL | fn f(){(print!(á
15+
| -- - ^
16+
| || |
17+
| || unclosed delimiter
18+
| |unclosed delimiter
19+
| unclosed delimiter
20+
21+
error: this file contains an unclosed delimiter
22+
--> $DIR/issue-104897.rs:6:18
23+
|
24+
LL | fn f(){(print!(á
25+
| -- - ^
26+
| || |
27+
| || unclosed delimiter
28+
| |unclosed delimiter
29+
| unclosed delimiter
30+
31+
error: format argument must be a string literal
32+
--> $DIR/issue-104897.rs:6:16
33+
|
34+
LL | fn f(){(print!(á
35+
| ^
36+
|
37+
help: you might be missing a string literal to format with
38+
|
39+
LL | fn f(){(print!("{}", á
40+
| +++++
41+
42+
error: aborting due to 4 previous errors
43+

0 commit comments

Comments
 (0)