Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
8f40d09e
R
Rust
项目概览
int
/
Rust
12 个月 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8f40d09e
编写于
6月 03, 2023
作者:
M
Morten Lohne
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add tests for const comparisons that compare two different types
上级
08e1333f
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
115 addition
and
26 deletion
+115
-26
tests/ui/double_const_comparisons.rs
tests/ui/double_const_comparisons.rs
+41
-0
tests/ui/double_const_comparisons.stderr
tests/ui/double_const_comparisons.stderr
+74
-26
未找到文件。
tests/ui/double_const_comparisons.rs
浏览文件 @
8f40d09e
...
...
@@ -8,8 +8,37 @@
const
STATUS_BAD_REQUEST
:
u16
=
400
;
const
STATUS_SERVER_ERROR
:
u16
=
500
;
struct
Status
{
code
:
u16
,
}
impl
PartialEq
<
u16
>
for
Status
{
fn
eq
(
&
self
,
other
:
&
u16
)
->
bool
{
self
.code
==
*
other
}
}
impl
PartialOrd
<
u16
>
for
Status
{
fn
partial_cmp
(
&
self
,
other
:
&
u16
)
->
Option
<
std
::
cmp
::
Ordering
>
{
self
.code
.partial_cmp
(
other
)
}
}
impl
PartialEq
<
Status
>
for
u16
{
fn
eq
(
&
self
,
other
:
&
Status
)
->
bool
{
*
self
==
other
.code
}
}
impl
PartialOrd
<
Status
>
for
u16
{
fn
partial_cmp
(
&
self
,
other
:
&
Status
)
->
Option
<
std
::
cmp
::
Ordering
>
{
self
.partial_cmp
(
&
other
.code
)
}
}
fn
main
()
{
let
status_code
=
500
;
// Value doesn't matter for the lint
let
status
=
Status
{
code
:
status_code
};
status_code
>=
400
&&
status_code
<
500
;
// Correct
status_code
<=
400
&&
status_code
>
500
;
...
...
@@ -22,12 +51,24 @@ fn main() {
status_code
<=
u16
::
MIN
+
1
&&
status_code
>
STATUS_SERVER_ERROR
;
status_code
<
STATUS_SERVER_ERROR
&&
status_code
>
STATUS_SERVER_ERROR
;
// Comparing two different types, via the `impl PartialOrd<u16> for Status`
status
<
{
400
}
&&
status
>
{
500
};
status
<
STATUS_BAD_REQUEST
&&
status
>
STATUS_SERVER_ERROR
;
status
<=
u16
::
MIN
+
1
&&
status
>
STATUS_SERVER_ERROR
;
status
<
STATUS_SERVER_ERROR
&&
status
>
STATUS_SERVER_ERROR
;
// Yoda conditions
500
<=
status_code
&&
600
>
status_code
;
// Correct
500
<=
status_code
&&
status_code
<=
600
;
// Correct
500
>=
status_code
&&
600
<
status_code
;
// Incorrect
500
>=
status_code
&&
status_code
>
600
;
// Incorrect
// Yoda conditions, comparing two different types
500
<=
status
&&
600
>
status
;
// Correct
500
<=
status
&&
status
<=
600
;
// Correct
500
>=
status
&&
600
<
status
;
// Incorrect
500
>=
status
&&
status
>
600
;
// Incorrect
// Expressions where one of the sides has no effect
status_code
<
200
&&
status_code
<=
299
;
status_code
>
200
&&
status_code
>=
299
;
...
...
tests/ui/double_const_comparisons.stderr
浏览文件 @
8f40d09e
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
15
:5
--> $DIR/double_const_comparisons.rs:
44
:5
|
LL | status_code <= 400 && status_code > 500;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
@@ -8,7 +8,7 @@ LL | status_code <= 400 && status_code > 500;
= note: `-D clippy::impossible-double-const-comparisons` implied by `-D warnings`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
16
:5
--> $DIR/double_const_comparisons.rs:
45
:5
|
LL | status_code > 500 && status_code < 400;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
@@ -16,7 +16,7 @@ LL | status_code > 500 && status_code < 400;
= note: since `500` > `400`, the expression evaluates to false for any value of `status_code`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
17
:5
--> $DIR/double_const_comparisons.rs:
46
:5
|
LL | status_code < 500 && status_code > 500;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
@@ -24,7 +24,7 @@ LL | status_code < 500 && status_code > 500;
= note: `status_code` cannot simultaneously be greater than and less than `500`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
20
:5
--> $DIR/double_const_comparisons.rs:
49
:5
|
LL | status_code < { 400 } && status_code > { 500 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
@@ -32,7 +32,7 @@ LL | status_code < { 400 } && status_code > { 500 };
= note: since `{ 400 }` < `{ 500 }`, the expression evaluates to false for any value of `status_code`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
21
:5
--> $DIR/double_const_comparisons.rs:
50
:5
|
LL | status_code < STATUS_BAD_REQUEST && status_code > STATUS_SERVER_ERROR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
@@ -40,7 +40,7 @@ LL | status_code < STATUS_BAD_REQUEST && status_code > STATUS_SERVER_ERROR;
= note: since `STATUS_BAD_REQUEST` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status_code`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
22
:5
--> $DIR/double_const_comparisons.rs:
51
:5
|
LL | status_code <= u16::MIN + 1 && status_code > STATUS_SERVER_ERROR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
@@ -48,7 +48,7 @@ LL | status_code <= u16::MIN + 1 && status_code > STATUS_SERVER_ERROR;
= note: since `u16::MIN + 1` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status_code`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
23
:5
--> $DIR/double_const_comparisons.rs:
52
:5
|
LL | status_code < STATUS_SERVER_ERROR && status_code > STATUS_SERVER_ERROR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
@@ -56,7 +56,39 @@ LL | status_code < STATUS_SERVER_ERROR && status_code > STATUS_SERVER_ERROR;
= note: `status_code` cannot simultaneously be greater than and less than `STATUS_SERVER_ERROR`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:28:5
--> $DIR/double_const_comparisons.rs:55:5
|
LL | status < { 400 } && status > { 500 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: since `{ 400 }` < `{ 500 }`, the expression evaluates to false for any value of `status`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:56:5
|
LL | status < STATUS_BAD_REQUEST && status > STATUS_SERVER_ERROR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: since `STATUS_BAD_REQUEST` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:57:5
|
LL | status <= u16::MIN + 1 && status > STATUS_SERVER_ERROR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: since `u16::MIN + 1` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:58:5
|
LL | status < STATUS_SERVER_ERROR && status > STATUS_SERVER_ERROR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `status` cannot simultaneously be greater than and less than `STATUS_SERVER_ERROR`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:63:5
|
LL | 500 >= status_code && 600 < status_code; // Incorrect
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
@@ -64,88 +96,104 @@ LL | 500 >= status_code && 600 < status_code; // Incorrect
= note: since `500` < `600`, the expression evaluates to false for any value of `status_code`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
29
:5
--> $DIR/double_const_comparisons.rs:
64
:5
|
LL | 500 >= status_code && status_code > 600; // Incorrect
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: since `500` < `600`, the expression evaluates to false for any value of `status_code`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:69:5
|
LL | 500 >= status && 600 < status; // Incorrect
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: since `500` < `600`, the expression evaluates to false for any value of `status`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:70:5
|
LL | 500 >= status && status > 600; // Incorrect
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: since `500` < `600`, the expression evaluates to false for any value of `status`
error: right-hand side of `&&` operator has no effect
--> $DIR/double_const_comparisons.rs:
32
:5
--> $DIR/double_const_comparisons.rs:
73
:5
|
LL | status_code < 200 && status_code <= 299;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: `if `status_code < 200` evaluates to true, status_code <= 299` will always evaluate to true as well
--> $DIR/double_const_comparisons.rs:
32
:23
--> $DIR/double_const_comparisons.rs:
73
:23
|
LL | status_code < 200 && status_code <= 299;
| ^^^^^^^^^^^^^^^^^^^^^
= note: `-D clippy::ineffective-double-const-comparisons` implied by `-D warnings`
error: left-hand side of `&&` operator has no effect
--> $DIR/double_const_comparisons.rs:
33
:5
--> $DIR/double_const_comparisons.rs:
74
:5
|
LL | status_code > 200 && status_code >= 299;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: `if `status_code >= 299` evaluates to true, status_code > 200` will always evaluate to true as well
--> $DIR/double_const_comparisons.rs:
33
:5
--> $DIR/double_const_comparisons.rs:
74
:5
|
LL | status_code > 200 && status_code >= 299;
| ^^^^^^^^^^^^^^^^^^^^^
error: left-hand side of `&&` operator has no effect
--> $DIR/double_const_comparisons.rs:
35
:5
--> $DIR/double_const_comparisons.rs:
76
:5
|
LL | status_code >= 500 && status_code > 500; // Useless left
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: `if `status_code > 500` evaluates to true, status_code >= 500` will always evaluate to true as well
--> $DIR/double_const_comparisons.rs:
35
:5
--> $DIR/double_const_comparisons.rs:
76
:5
|
LL | status_code >= 500 && status_code > 500; // Useless left
| ^^^^^^^^^^^^^^^^^^^^^^
error: right-hand side of `&&` operator has no effect
--> $DIR/double_const_comparisons.rs:
36
:5
--> $DIR/double_const_comparisons.rs:
77
:5
|
LL | status_code > 500 && status_code >= 500; // Useless right
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: `if `status_code > 500` evaluates to true, status_code >= 500` will always evaluate to true as well
--> $DIR/double_const_comparisons.rs:
36
:23
--> $DIR/double_const_comparisons.rs:
77
:23
|
LL | status_code > 500 && status_code >= 500; // Useless right
| ^^^^^^^^^^^^^^^^^^^^^
error: left-hand side of `&&` operator has no effect
--> $DIR/double_const_comparisons.rs:
37
:5
--> $DIR/double_const_comparisons.rs:
78
:5
|
LL | status_code <= 500 && status_code < 500; // Useless left
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: `if `status_code < 500` evaluates to true, status_code <= 500` will always evaluate to true as well
--> $DIR/double_const_comparisons.rs:
37
:5
--> $DIR/double_const_comparisons.rs:
78
:5
|
LL | status_code <= 500 && status_code < 500; // Useless left
| ^^^^^^^^^^^^^^^^^^^^^^
error: right-hand side of `&&` operator has no effect
--> $DIR/double_const_comparisons.rs:
38
:5
--> $DIR/double_const_comparisons.rs:
79
:5
|
LL | status_code < 500 && status_code <= 500; // Useless right
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: `if `status_code < 500` evaluates to true, status_code <= 500` will always evaluate to true as well
--> $DIR/double_const_comparisons.rs:
38
:23
--> $DIR/double_const_comparisons.rs:
79
:23
|
LL | status_code < 500 && status_code <= 500; // Useless right
| ^^^^^^^^^^^^^^^^^^^^^
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
42
:5
--> $DIR/double_const_comparisons.rs:
83
:5
|
LL | name < "Jennifer" && name > "Shannon";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
@@ -153,7 +201,7 @@ LL | name < "Jennifer" && name > "Shannon";
= note: since `"Jennifer"` < `"Shannon"`, the expression evaluates to false for any value of `name`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
45
:5
--> $DIR/double_const_comparisons.rs:
86
:5
|
LL | numbers < [3, 4] && numbers > [5, 6];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
@@ -161,7 +209,7 @@ LL | numbers < [3, 4] && numbers > [5, 6];
= note: since `[3, 4]` < `[5, 6]`, the expression evaluates to false for any value of `numbers`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
48
:5
--> $DIR/double_const_comparisons.rs:
89
:5
|
LL | letter < 'b' && letter > 'c';
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
@@ -169,12 +217,12 @@ LL | letter < 'b' && letter > 'c';
= note: since `'b'` < `'c'`, the expression evaluates to false for any value of `letter`
error: boolean expression will never evaluate to 'true'
--> $DIR/double_const_comparisons.rs:
51
:5
--> $DIR/double_const_comparisons.rs:
92
:5
|
LL | area < std::f32::consts::E && area > std::f32::consts::PI;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: since `std::f32::consts::E` < `std::f32::consts::PI`, the expression evaluates to false for any value of `area`
error: aborting due to
19
previous errors
error: aborting due to
25
previous errors
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录