Skip to content

Commit c56dd3d

Browse files
committed
Auto merge of rust-lang#10766 - samueltardieu:issue-10710, r=Manishearth
needless_bool: do not simplify code if it loses comments Fix rust-lang#10710 changelog: [`needless_bool`]: do not simplify code if it loses comments
2 parents 1407c76 + c5d4b04 commit c56dd3d

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

clippy_lints/src/needless_bool.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn is_parent_stmt(cx: &LateContext<'_>, id: HirId) -> bool {
146146
impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
147147
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
148148
use self::Expression::{Bool, RetBool};
149-
if e.span.from_expansion() {
149+
if e.span.from_expansion() || !span_extract_comment(cx.tcx.sess.source_map(), e.span).is_empty() {
150150
return;
151151
}
152152
if let Some(higher::If {
@@ -209,8 +209,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
209209
}
210210
if let Some((lhs_a, a)) = fetch_assign(then) &&
211211
let Some((lhs_b, b)) = fetch_assign(r#else) &&
212-
SpanlessEq::new(cx).eq_expr(lhs_a, lhs_b) &&
213-
span_extract_comment(cx.tcx.sess.source_map(), e.span).is_empty()
212+
SpanlessEq::new(cx).eq_expr(lhs_a, lhs_b)
214213
{
215214
let mut applicability = Applicability::MachineApplicable;
216215
let cond = Sugg::hir_with_applicability(cx, cond, "..", &mut applicability);

tests/ui/needless_bool/fixable.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ fn main() {
6363
needless_bool2(x);
6464
needless_bool3(x);
6565
needless_bool_condition();
66+
67+
if a == b {
68+
true
69+
} else {
70+
// Do not lint as this comment might be important
71+
false
72+
};
6673
}
6774

6875
fn bool_ret3(x: bool) -> bool {

tests/ui/needless_bool/fixable.rs

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ fn main() {
9999
needless_bool2(x);
100100
needless_bool3(x);
101101
needless_bool_condition();
102+
103+
if a == b {
104+
true
105+
} else {
106+
// Do not lint as this comment might be important
107+
false
108+
};
102109
}
103110

104111
fn bool_ret3(x: bool) -> bool {

tests/ui/needless_bool/fixable.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ LL | | };
9191
| |_____^ help: you can reduce it to: `a < b`
9292

9393
error: this if-then-else expression returns a bool literal
94-
--> $DIR/fixable.rs:105:5
94+
--> $DIR/fixable.rs:112:5
9595
|
9696
LL | / if x {
9797
LL | | return true;
@@ -101,7 +101,7 @@ LL | | };
101101
| |_____^ help: you can reduce it to: `return x`
102102

103103
error: this if-then-else expression returns a bool literal
104-
--> $DIR/fixable.rs:113:5
104+
--> $DIR/fixable.rs:120:5
105105
|
106106
LL | / if x {
107107
LL | | return false;
@@ -111,7 +111,7 @@ LL | | };
111111
| |_____^ help: you can reduce it to: `return !x`
112112

113113
error: this if-then-else expression returns a bool literal
114-
--> $DIR/fixable.rs:121:5
114+
--> $DIR/fixable.rs:128:5
115115
|
116116
LL | / if x && y {
117117
LL | | return true;
@@ -121,7 +121,7 @@ LL | | };
121121
| |_____^ help: you can reduce it to: `return x && y`
122122

123123
error: this if-then-else expression returns a bool literal
124-
--> $DIR/fixable.rs:129:5
124+
--> $DIR/fixable.rs:136:5
125125
|
126126
LL | / if x && y {
127127
LL | | return false;
@@ -131,33 +131,33 @@ LL | | };
131131
| |_____^ help: you can reduce it to: `return !(x && y)`
132132

133133
error: equality checks against true are unnecessary
134-
--> $DIR/fixable.rs:137:8
134+
--> $DIR/fixable.rs:144:8
135135
|
136136
LL | if x == true {};
137137
| ^^^^^^^^^ help: try simplifying it as shown: `x`
138138
|
139139
= note: `-D clippy::bool-comparison` implied by `-D warnings`
140140

141141
error: equality checks against false can be replaced by a negation
142-
--> $DIR/fixable.rs:141:8
142+
--> $DIR/fixable.rs:148:8
143143
|
144144
LL | if x == false {};
145145
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
146146

147147
error: equality checks against true are unnecessary
148-
--> $DIR/fixable.rs:151:8
148+
--> $DIR/fixable.rs:158:8
149149
|
150150
LL | if x == true {};
151151
| ^^^^^^^^^ help: try simplifying it as shown: `x`
152152

153153
error: equality checks against false can be replaced by a negation
154-
--> $DIR/fixable.rs:152:8
154+
--> $DIR/fixable.rs:159:8
155155
|
156156
LL | if x == false {};
157157
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
158158

159159
error: this if-then-else expression returns a bool literal
160-
--> $DIR/fixable.rs:161:12
160+
--> $DIR/fixable.rs:168:12
161161
|
162162
LL | } else if returns_bool() {
163163
| ____________^
@@ -168,7 +168,7 @@ LL | | };
168168
| |_____^ help: you can reduce it to: `{ !returns_bool() }`
169169

170170
error: this if-then-else expression returns a bool literal
171-
--> $DIR/fixable.rs:174:5
171+
--> $DIR/fixable.rs:181:5
172172
|
173173
LL | / if unsafe { no(4) } & 1 != 0 {
174174
LL | | true
@@ -178,13 +178,13 @@ LL | | };
178178
| |_____^ help: you can reduce it to: `(unsafe { no(4) } & 1 != 0)`
179179

180180
error: this if-then-else expression returns a bool literal
181-
--> $DIR/fixable.rs:179:30
181+
--> $DIR/fixable.rs:186:30
182182
|
183183
LL | let _brackets_unneeded = if unsafe { no(4) } & 1 != 0 { true } else { false };
184184
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `unsafe { no(4) } & 1 != 0`
185185

186186
error: this if-then-else expression returns a bool literal
187-
--> $DIR/fixable.rs:182:9
187+
--> $DIR/fixable.rs:189:9
188188
|
189189
LL | if unsafe { no(4) } & 1 != 0 { true } else { false }
190190
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `(unsafe { no(4) } & 1 != 0)`

0 commit comments

Comments
 (0)