Skip to content

Commit 4e31c8c

Browse files
committed
Auto merge of rust-lang#9389 - lukaslueg:penmacro, r=llogiq
Don't lint literal `None` from expansion This addresses rust-lang/rust-clippy#9288 (comment): If the literal `None` is from expansion, we never lint. This is correct because e.g. replacing the call to `option_env!` with whatever that macro expanded to at the time of linting is certainly wrong. changelog: Don't lint [`partialeq_to_none`] for macro-expansions
2 parents 8d9da4d + c542f1f commit 4e31c8c

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

clippy_lints/src/partialeq_to_none.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ impl<'tcx> LateLintPass<'tcx> for PartialeqToNone {
5353

5454
// If the expression is a literal `Option::None`
5555
let is_none_ctor = |expr: &Expr<'_>| {
56-
matches!(&peel_hir_expr_refs(expr).0.kind,
56+
!expr.span.from_expansion()
57+
&& matches!(&peel_hir_expr_refs(expr).0.kind,
5758
ExprKind::Path(p) if is_lang_ctor(cx, p, LangItem::OptionNone))
5859
};
5960

tests/ui/partialeq_to_none.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ fn optref() -> &'static &'static Option<()> {
2626
&&None
2727
}
2828

29+
pub fn macro_expansion() {
30+
macro_rules! foo {
31+
() => {
32+
None::<()>
33+
};
34+
}
35+
36+
let _ = foobar() == foo!();
37+
let _ = foo!() == foobar();
38+
let _ = foo!() == foo!();
39+
}
40+
2941
fn main() {
3042
let x = Some(0);
3143

tests/ui/partialeq_to_none.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ fn optref() -> &'static &'static Option<()> {
2626
&&None
2727
}
2828

29+
pub fn macro_expansion() {
30+
macro_rules! foo {
31+
() => {
32+
None::<()>
33+
};
34+
}
35+
36+
let _ = foobar() == foo!();
37+
let _ = foo!() == foobar();
38+
let _ = foo!() == foo!();
39+
}
40+
2941
fn main() {
3042
let x = Some(0);
3143

tests/ui/partialeq_to_none.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,55 @@ LL | if f != None { "yay" } else { "nay" }
77
= note: `-D clippy::partialeq-to-none` implied by `-D warnings`
88

99
error: binary comparison to literal `Option::None`
10-
--> $DIR/partialeq_to_none.rs:32:13
10+
--> $DIR/partialeq_to_none.rs:44:13
1111
|
1212
LL | let _ = x == None;
1313
| ^^^^^^^^^ help: use `Option::is_none()` instead: `x.is_none()`
1414

1515
error: binary comparison to literal `Option::None`
16-
--> $DIR/partialeq_to_none.rs:33:13
16+
--> $DIR/partialeq_to_none.rs:45:13
1717
|
1818
LL | let _ = x != None;
1919
| ^^^^^^^^^ help: use `Option::is_some()` instead: `x.is_some()`
2020

2121
error: binary comparison to literal `Option::None`
22-
--> $DIR/partialeq_to_none.rs:34:13
22+
--> $DIR/partialeq_to_none.rs:46:13
2323
|
2424
LL | let _ = None == x;
2525
| ^^^^^^^^^ help: use `Option::is_none()` instead: `x.is_none()`
2626

2727
error: binary comparison to literal `Option::None`
28-
--> $DIR/partialeq_to_none.rs:35:13
28+
--> $DIR/partialeq_to_none.rs:47:13
2929
|
3030
LL | let _ = None != x;
3131
| ^^^^^^^^^ help: use `Option::is_some()` instead: `x.is_some()`
3232

3333
error: binary comparison to literal `Option::None`
34-
--> $DIR/partialeq_to_none.rs:37:8
34+
--> $DIR/partialeq_to_none.rs:49:8
3535
|
3636
LL | if foobar() == None {}
3737
| ^^^^^^^^^^^^^^^^ help: use `Option::is_none()` instead: `foobar().is_none()`
3838

3939
error: binary comparison to literal `Option::None`
40-
--> $DIR/partialeq_to_none.rs:39:8
40+
--> $DIR/partialeq_to_none.rs:51:8
4141
|
4242
LL | if bar().ok() != None {}
4343
| ^^^^^^^^^^^^^^^^^^ help: use `Option::is_some()` instead: `bar().ok().is_some()`
4444

4545
error: binary comparison to literal `Option::None`
46-
--> $DIR/partialeq_to_none.rs:41:13
46+
--> $DIR/partialeq_to_none.rs:53:13
4747
|
4848
LL | let _ = Some(1 + 2) != None;
4949
| ^^^^^^^^^^^^^^^^^^^ help: use `Option::is_some()` instead: `Some(1 + 2).is_some()`
5050

5151
error: binary comparison to literal `Option::None`
52-
--> $DIR/partialeq_to_none.rs:43:13
52+
--> $DIR/partialeq_to_none.rs:55:13
5353
|
5454
LL | let _ = { Some(0) } == None;
5555
| ^^^^^^^^^^^^^^^^^^^ help: use `Option::is_none()` instead: `{ Some(0) }.is_none()`
5656

5757
error: binary comparison to literal `Option::None`
58-
--> $DIR/partialeq_to_none.rs:45:13
58+
--> $DIR/partialeq_to_none.rs:57:13
5959
|
6060
LL | let _ = {
6161
| _____________^
@@ -77,31 +77,31 @@ LL ~ }.is_some();
7777
|
7878

7979
error: binary comparison to literal `Option::None`
80-
--> $DIR/partialeq_to_none.rs:55:13
80+
--> $DIR/partialeq_to_none.rs:67:13
8181
|
8282
LL | let _ = optref() == &&None;
8383
| ^^^^^^^^^^^^^^^^^^ help: use `Option::is_none()` instead: `optref().is_none()`
8484

8585
error: binary comparison to literal `Option::None`
86-
--> $DIR/partialeq_to_none.rs:56:13
86+
--> $DIR/partialeq_to_none.rs:68:13
8787
|
8888
LL | let _ = &&None != optref();
8989
| ^^^^^^^^^^^^^^^^^^ help: use `Option::is_some()` instead: `optref().is_some()`
9090

9191
error: binary comparison to literal `Option::None`
92-
--> $DIR/partialeq_to_none.rs:57:13
92+
--> $DIR/partialeq_to_none.rs:69:13
9393
|
9494
LL | let _ = **optref() == None;
9595
| ^^^^^^^^^^^^^^^^^^ help: use `Option::is_none()` instead: `optref().is_none()`
9696

9797
error: binary comparison to literal `Option::None`
98-
--> $DIR/partialeq_to_none.rs:58:13
98+
--> $DIR/partialeq_to_none.rs:70:13
9999
|
100100
LL | let _ = &None != *optref();
101101
| ^^^^^^^^^^^^^^^^^^ help: use `Option::is_some()` instead: `optref().is_some()`
102102

103103
error: binary comparison to literal `Option::None`
104-
--> $DIR/partialeq_to_none.rs:61:13
104+
--> $DIR/partialeq_to_none.rs:73:13
105105
|
106106
LL | let _ = None != *x;
107107
| ^^^^^^^^^^ help: use `Option::is_some()` instead: `(*x).is_some()`

0 commit comments

Comments
 (0)