Skip to content

Commit 4374059

Browse files
committed
Treat some constant values as though they were written inline.
1 parent a44bb07 commit 4374059

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed

clippy_utils/src/consts.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::{BinOp, BinOpKind, Block, ConstBlock, Expr, ExprKind, HirId, Item
1010
use rustc_lexer::tokenize;
1111
use rustc_lint::LateContext;
1212
use rustc_middle::mir::interpret::{alloc_range, Scalar};
13-
use rustc_middle::ty::{self, EarlyBinder, FloatTy, GenericArgsRef, List, ScalarInt, Ty, TyCtxt};
13+
use rustc_middle::ty::{self, EarlyBinder, FloatTy, GenericArgsRef, IntTy, List, ScalarInt, Ty, TyCtxt, UintTy};
1414
use rustc_middle::{bug, mir, span_bug};
1515
use rustc_span::symbol::{Ident, Symbol};
1616
use rustc_span::SyntaxContext;
@@ -457,7 +457,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
457457
fn fetch_path(&mut self, qpath: &QPath<'_>, id: HirId, ty: Ty<'tcx>) -> Option<Constant<'tcx>> {
458458
let res = self.typeck_results.qpath_res(qpath, id);
459459
match res {
460-
Res::Def(DefKind::Const | DefKind::AssocConst, def_id) => {
460+
Res::Def(def_kind @ (DefKind::Const | DefKind::AssocConst), def_id) => {
461461
// Check if this constant is based on `cfg!(..)`,
462462
// which is NOT constant for our purposes.
463463
if let Some(node) = self.lcx.tcx.hir().get_if_local(def_id)
@@ -488,7 +488,25 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
488488
.ok()
489489
.map(|val| rustc_middle::mir::Const::from_value(val, ty))?;
490490
let result = mir_to_const(self.lcx, result)?;
491-
self.source = ConstantSource::Constant;
491+
492+
if matches!(def_kind, DefKind::AssocConst)
493+
&& let impl_id = self.lcx.tcx.parent(def_id)
494+
&& matches!(
495+
self.lcx.tcx.opt_def_kind(impl_id),
496+
Some(DefKind::Impl { of_trait: false })
497+
)
498+
&& matches!(
499+
self.lcx.tcx.type_of(impl_id).instantiate_identity().kind(),
500+
ty::Uint(UintTy::U8 | UintTy::U16 | UintTy::U32 | UintTy::U64 | UintTy::U128)
501+
| ty::Int(IntTy::I8 | IntTy::I16 | IntTy::I32 | IntTy::I64 | IntTy::I128)
502+
| ty::Char
503+
| ty::Float(_)
504+
)
505+
{
506+
// The associated constants of these types will never change.
507+
} else {
508+
self.source = ConstantSource::Constant;
509+
}
492510
Some(result)
493511
},
494512
_ => None,

tests/ui/const_comparisons.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(clippy::no_effect)]
55
#![allow(clippy::short_circuit_statement)]
66
#![allow(clippy::manual_range_contains)]
7+
#![allow(clippy::identity_op)]
78

89
const STATUS_BAD_REQUEST: u16 = 400;
910
const STATUS_SERVER_ERROR: u16 = 500;

tests/ui/const_comparisons.stderr

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: boolean expression will never evaluate to 'true'
2-
--> $DIR/const_comparisons.rs:45:5
2+
--> $DIR/const_comparisons.rs:46:5
33
|
44
LL | status_code <= 400 && status_code > 500;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -9,217 +9,217 @@ LL | status_code <= 400 && status_code > 500;
99
= help: to override `-D warnings` add `#[allow(clippy::impossible_comparisons)]`
1010

1111
error: boolean expression will never evaluate to 'true'
12-
--> $DIR/const_comparisons.rs:48:5
12+
--> $DIR/const_comparisons.rs:49:5
1313
|
1414
LL | status_code > 500 && status_code < 400;
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616
|
1717
= note: since `500` > `400`, the expression evaluates to false for any value of `status_code`
1818

1919
error: boolean expression will never evaluate to 'true'
20-
--> $DIR/const_comparisons.rs:51:5
20+
--> $DIR/const_comparisons.rs:52:5
2121
|
2222
LL | status_code < 500 && status_code > 500;
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424
|
2525
= note: `status_code` cannot simultaneously be greater than and less than `500`
2626

2727
error: boolean expression will never evaluate to 'true'
28-
--> $DIR/const_comparisons.rs:56:5
28+
--> $DIR/const_comparisons.rs:57:5
2929
|
3030
LL | status_code < { 400 } && status_code > { 500 };
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232
|
3333
= note: since `{ 400 }` < `{ 500 }`, the expression evaluates to false for any value of `status_code`
3434

3535
error: boolean expression will never evaluate to 'true'
36-
--> $DIR/const_comparisons.rs:59:5
36+
--> $DIR/const_comparisons.rs:60:5
3737
|
3838
LL | status_code < STATUS_BAD_REQUEST && status_code > STATUS_SERVER_ERROR;
3939
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4040
|
4141
= note: since `STATUS_BAD_REQUEST` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status_code`
4242

4343
error: boolean expression will never evaluate to 'true'
44-
--> $DIR/const_comparisons.rs:62:5
44+
--> $DIR/const_comparisons.rs:63:5
4545
|
4646
LL | status_code <= u16::MIN + 1 && status_code > STATUS_SERVER_ERROR;
4747
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4848
|
4949
= note: since `u16::MIN + 1` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status_code`
5050

5151
error: boolean expression will never evaluate to 'true'
52-
--> $DIR/const_comparisons.rs:65:5
52+
--> $DIR/const_comparisons.rs:66:5
5353
|
5454
LL | status_code < STATUS_SERVER_ERROR && status_code > STATUS_SERVER_ERROR;
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5656
|
5757
= note: `status_code` cannot simultaneously be greater than and less than `STATUS_SERVER_ERROR`
5858

5959
error: boolean expression will never evaluate to 'true'
60-
--> $DIR/const_comparisons.rs:70:5
60+
--> $DIR/const_comparisons.rs:71:5
6161
|
6262
LL | status < { 400 } && status > { 500 };
6363
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6464
|
6565
= note: since `{ 400 }` < `{ 500 }`, the expression evaluates to false for any value of `status`
6666

6767
error: boolean expression will never evaluate to 'true'
68-
--> $DIR/const_comparisons.rs:73:5
68+
--> $DIR/const_comparisons.rs:74:5
6969
|
7070
LL | status < STATUS_BAD_REQUEST && status > STATUS_SERVER_ERROR;
7171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7272
|
7373
= note: since `STATUS_BAD_REQUEST` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status`
7474

7575
error: boolean expression will never evaluate to 'true'
76-
--> $DIR/const_comparisons.rs:76:5
76+
--> $DIR/const_comparisons.rs:77:5
7777
|
7878
LL | status <= u16::MIN + 1 && status > STATUS_SERVER_ERROR;
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8080
|
8181
= note: since `u16::MIN + 1` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status`
8282

8383
error: boolean expression will never evaluate to 'true'
84-
--> $DIR/const_comparisons.rs:79:5
84+
--> $DIR/const_comparisons.rs:80:5
8585
|
8686
LL | status < STATUS_SERVER_ERROR && status > STATUS_SERVER_ERROR;
8787
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8888
|
8989
= note: `status` cannot simultaneously be greater than and less than `STATUS_SERVER_ERROR`
9090

9191
error: boolean expression will never evaluate to 'true'
92-
--> $DIR/const_comparisons.rs:89:5
92+
--> $DIR/const_comparisons.rs:90:5
9393
|
9494
LL | 500 >= status_code && 600 < status_code;
9595
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9696
|
9797
= note: since `500` < `600`, the expression evaluates to false for any value of `status_code`
9898

9999
error: boolean expression will never evaluate to 'true'
100-
--> $DIR/const_comparisons.rs:93:5
100+
--> $DIR/const_comparisons.rs:94:5
101101
|
102102
LL | 500 >= status_code && status_code > 600;
103103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
104104
|
105105
= note: since `500` < `600`, the expression evaluates to false for any value of `status_code`
106106

107107
error: boolean expression will never evaluate to 'true'
108-
--> $DIR/const_comparisons.rs:103:5
108+
--> $DIR/const_comparisons.rs:104:5
109109
|
110110
LL | 500 >= status && 600 < status;
111111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
112112
|
113113
= note: since `500` < `600`, the expression evaluates to false for any value of `status`
114114

115115
error: boolean expression will never evaluate to 'true'
116-
--> $DIR/const_comparisons.rs:107:5
116+
--> $DIR/const_comparisons.rs:108:5
117117
|
118118
LL | 500 >= status && status > 600;
119119
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120120
|
121121
= note: since `500` < `600`, the expression evaluates to false for any value of `status`
122122

123123
error: right-hand side of `&&` operator has no effect
124-
--> $DIR/const_comparisons.rs:112:5
124+
--> $DIR/const_comparisons.rs:113:5
125125
|
126126
LL | status_code < 200 && status_code <= 299;
127127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128128
|
129129
note: `if `status_code < 200` evaluates to true, status_code <= 299` will always evaluate to true as well
130-
--> $DIR/const_comparisons.rs:112:23
130+
--> $DIR/const_comparisons.rs:113:23
131131
|
132132
LL | status_code < 200 && status_code <= 299;
133133
| ^^^^^^^^^^^^^^^^^^^^^
134134
= note: `-D clippy::redundant-comparisons` implied by `-D warnings`
135135
= help: to override `-D warnings` add `#[allow(clippy::redundant_comparisons)]`
136136

137137
error: left-hand side of `&&` operator has no effect
138-
--> $DIR/const_comparisons.rs:114:5
138+
--> $DIR/const_comparisons.rs:115:5
139139
|
140140
LL | status_code > 200 && status_code >= 299;
141141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
142142
|
143143
note: `if `status_code >= 299` evaluates to true, status_code > 200` will always evaluate to true as well
144-
--> $DIR/const_comparisons.rs:114:5
144+
--> $DIR/const_comparisons.rs:115:5
145145
|
146146
LL | status_code > 200 && status_code >= 299;
147147
| ^^^^^^^^^^^^^^^^^^^^^
148148

149149
error: left-hand side of `&&` operator has no effect
150-
--> $DIR/const_comparisons.rs:118:5
150+
--> $DIR/const_comparisons.rs:119:5
151151
|
152152
LL | status_code >= 500 && status_code > 500;
153153
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
154154
|
155155
note: `if `status_code > 500` evaluates to true, status_code >= 500` will always evaluate to true as well
156-
--> $DIR/const_comparisons.rs:118:5
156+
--> $DIR/const_comparisons.rs:119:5
157157
|
158158
LL | status_code >= 500 && status_code > 500;
159159
| ^^^^^^^^^^^^^^^^^^^^^^
160160

161161
error: right-hand side of `&&` operator has no effect
162-
--> $DIR/const_comparisons.rs:121:5
162+
--> $DIR/const_comparisons.rs:122:5
163163
|
164164
LL | status_code > 500 && status_code >= 500;
165165
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
166166
|
167167
note: `if `status_code > 500` evaluates to true, status_code >= 500` will always evaluate to true as well
168-
--> $DIR/const_comparisons.rs:121:23
168+
--> $DIR/const_comparisons.rs:122:23
169169
|
170170
LL | status_code > 500 && status_code >= 500;
171171
| ^^^^^^^^^^^^^^^^^^^^^
172172

173173
error: left-hand side of `&&` operator has no effect
174-
--> $DIR/const_comparisons.rs:124:5
174+
--> $DIR/const_comparisons.rs:125:5
175175
|
176176
LL | status_code <= 500 && status_code < 500;
177177
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
178178
|
179179
note: `if `status_code < 500` evaluates to true, status_code <= 500` will always evaluate to true as well
180-
--> $DIR/const_comparisons.rs:124:5
180+
--> $DIR/const_comparisons.rs:125:5
181181
|
182182
LL | status_code <= 500 && status_code < 500;
183183
| ^^^^^^^^^^^^^^^^^^^^^^
184184

185185
error: right-hand side of `&&` operator has no effect
186-
--> $DIR/const_comparisons.rs:127:5
186+
--> $DIR/const_comparisons.rs:128:5
187187
|
188188
LL | status_code < 500 && status_code <= 500;
189189
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
190190
|
191191
note: `if `status_code < 500` evaluates to true, status_code <= 500` will always evaluate to true as well
192-
--> $DIR/const_comparisons.rs:127:23
192+
--> $DIR/const_comparisons.rs:128:23
193193
|
194194
LL | status_code < 500 && status_code <= 500;
195195
| ^^^^^^^^^^^^^^^^^^^^^
196196

197197
error: boolean expression will never evaluate to 'true'
198-
--> $DIR/const_comparisons.rs:132:5
198+
--> $DIR/const_comparisons.rs:133:5
199199
|
200200
LL | name < "Jennifer" && name > "Shannon";
201201
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
202202
|
203203
= note: since `"Jennifer"` < `"Shannon"`, the expression evaluates to false for any value of `name`
204204

205205
error: boolean expression will never evaluate to 'true'
206-
--> $DIR/const_comparisons.rs:137:5
206+
--> $DIR/const_comparisons.rs:138:5
207207
|
208208
LL | numbers < [3, 4] && numbers > [5, 6];
209209
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
210210
|
211211
= note: since `[3, 4]` < `[5, 6]`, the expression evaluates to false for any value of `numbers`
212212

213213
error: boolean expression will never evaluate to 'true'
214-
--> $DIR/const_comparisons.rs:142:5
214+
--> $DIR/const_comparisons.rs:143:5
215215
|
216216
LL | letter < 'b' && letter > 'c';
217217
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
218218
|
219219
= note: since `'b'` < `'c'`, the expression evaluates to false for any value of `letter`
220220

221221
error: boolean expression will never evaluate to 'true'
222-
--> $DIR/const_comparisons.rs:147:5
222+
--> $DIR/const_comparisons.rs:148:5
223223
|
224224
LL | area < std::f32::consts::E && area > std::f32::consts::PI;
225225
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)