Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
c0ae75bf
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,发现更多精彩内容 >>
提交
c0ae75bf
编写于
8月 10, 2023
作者:
C
Catherine Flores
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Revert "New lint [`filter_map_bool_then`]"
This reverts commits
978b1daf
and
3235d9d6
.
上级
e3590fcc
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
0 addition
and
205 deletion
+0
-205
src/tools/clippy/CHANGELOG.md
src/tools/clippy/CHANGELOG.md
+0
-1
src/tools/clippy/clippy_lints/src/declared_lints.rs
src/tools/clippy/clippy_lints/src/declared_lints.rs
+0
-1
src/tools/clippy/clippy_lints/src/methods/filter_map_bool_then.rs
...s/clippy/clippy_lints/src/methods/filter_map_bool_then.rs
+0
-45
src/tools/clippy/clippy_lints/src/methods/mod.rs
src/tools/clippy/clippy_lints/src/methods/mod.rs
+0
-34
src/tools/clippy/clippy_utils/src/paths.rs
src/tools/clippy/clippy_utils/src/paths.rs
+0
-2
src/tools/clippy/tests/ui/filter_map_bool_then.fixed
src/tools/clippy/tests/ui/filter_map_bool_then.fixed
+0
-44
src/tools/clippy/tests/ui/filter_map_bool_then.rs
src/tools/clippy/tests/ui/filter_map_bool_then.rs
+0
-44
src/tools/clippy/tests/ui/filter_map_bool_then.stderr
src/tools/clippy/tests/ui/filter_map_bool_then.stderr
+0
-34
未找到文件。
src/tools/clippy/CHANGELOG.md
浏览文件 @
c0ae75bf
...
...
@@ -4844,7 +4844,6 @@ Released 2018-09-13
[
`field_reassign_with_default`
]:
https://rust-lang.github.io/rust-clippy/master/index.html#field_reassign_with_default
[
`filetype_is_file`
]:
https://rust-lang.github.io/rust-clippy/master/index.html#filetype_is_file
[
`filter_map`
]:
https://rust-lang.github.io/rust-clippy/master/index.html#filter_map
[
`filter_map_bool_then`
]:
https://rust-lang.github.io/rust-clippy/master/index.html#filter_map_bool_then
[
`filter_map_identity`
]:
https://rust-lang.github.io/rust-clippy/master/index.html#filter_map_identity
[
`filter_map_next`
]:
https://rust-lang.github.io/rust-clippy/master/index.html#filter_map_next
[
`filter_next`
]:
https://rust-lang.github.io/rust-clippy/master/index.html#filter_next
...
...
src/tools/clippy/clippy_lints/src/declared_lints.rs
浏览文件 @
c0ae75bf
...
...
@@ -337,7 +337,6 @@
crate
::
methods
::
EXPECT_USED_INFO
,
crate
::
methods
::
EXTEND_WITH_DRAIN_INFO
,
crate
::
methods
::
FILETYPE_IS_FILE_INFO
,
crate
::
methods
::
FILTER_MAP_BOOL_THEN_INFO
,
crate
::
methods
::
FILTER_MAP_IDENTITY_INFO
,
crate
::
methods
::
FILTER_MAP_NEXT_INFO
,
crate
::
methods
::
FILTER_NEXT_INFO
,
...
...
src/tools/clippy/clippy_lints/src/methods/filter_map_bool_then.rs
已删除
100644 → 0
浏览文件 @
e3590fcc
use
clippy_utils
::
diagnostics
::
span_lint_and_sugg
;
use
clippy_utils
::
paths
::
BOOL_THEN
;
use
clippy_utils
::
source
::
snippet_opt
;
use
clippy_utils
::
ty
::
is_copy
;
use
clippy_utils
::{
is_from_proc_macro
,
is_trait_method
,
match_def_path
,
peel_blocks
};
use
rustc_errors
::
Applicability
;
use
rustc_hir
::{
Expr
,
ExprKind
};
use
rustc_lint
::{
LateContext
,
LintContext
};
use
rustc_middle
::
lint
::
in_external_macro
;
use
rustc_span
::{
sym
,
Span
};
use
super
::
FILTER_MAP_BOOL_THEN
;
pub
(
super
)
fn
check
<
'tcx
>
(
cx
:
&
LateContext
<
'tcx
>
,
expr
:
&
'tcx
Expr
<
'tcx
>
,
arg
:
&
Expr
<
'_
>
,
call_span
:
Span
)
{
if
!
in_external_macro
(
cx
.sess
(),
expr
.span
)
&&
is_trait_method
(
cx
,
expr
,
sym
::
Iterator
)
&&
let
ExprKind
::
Closure
(
closure
)
=
arg
.kind
&&
let
body
=
cx
.tcx
.hir
()
.body
(
closure
.body
)
&&
let
value
=
peel_blocks
(
body
.value
)
// Indexing should be fine as `filter_map` always has 1 input, we unfortunately need both
// `inputs` and `params` here as we need both the type and the span
&&
let
param_ty
=
closure
.fn_decl.inputs
[
0
]
&&
let
param
=
body
.params
[
0
]
&&
is_copy
(
cx
,
cx
.typeck_results
()
.node_type
(
param_ty
.hir_id
)
.peel_refs
())
&&
let
ExprKind
::
MethodCall
(
_
,
recv
,
[
then_arg
],
_
)
=
value
.kind
&&
let
ExprKind
::
Closure
(
then_closure
)
=
then_arg
.kind
&&
let
then_body
=
peel_blocks
(
cx
.tcx
.hir
()
.body
(
then_closure
.body
)
.value
)
&&
let
Some
(
def_id
)
=
cx
.typeck_results
()
.type_dependent_def_id
(
value
.hir_id
)
&&
match_def_path
(
cx
,
def_id
,
&
BOOL_THEN
)
&&
!
is_from_proc_macro
(
cx
,
expr
)
&&
let
Some
(
param_snippet
)
=
snippet_opt
(
cx
,
param
.span
)
&&
let
Some
(
filter
)
=
snippet_opt
(
cx
,
recv
.span
)
&&
let
Some
(
map
)
=
snippet_opt
(
cx
,
then_body
.span
)
{
span_lint_and_sugg
(
cx
,
FILTER_MAP_BOOL_THEN
,
call_span
,
"usage of `bool::then` in `filter_map`"
,
"use `filter` then `map` instead"
,
format!
(
"filter(|&{param_snippet}| {filter}).map(|{param_snippet}| {map})"
),
Applicability
::
MachineApplicable
,
);
}
}
src/tools/clippy/clippy_lints/src/methods/mod.rs
浏览文件 @
c0ae75bf
...
...
@@ -21,7 +21,6 @@
mod
extend_with_drain
;
mod
filetype_is_file
;
mod
filter_map
;
mod
filter_map_bool_then
;
mod
filter_map_identity
;
mod
filter_map_next
;
mod
filter_next
;
...
...
@@ -3477,37 +3476,6 @@
"disallows `.skip(0)`"
}
declare_clippy_lint!
{
/// ### What it does
/// Checks for usage of `bool::then` in `Iterator::filter_map`.
///
/// ### Why is this bad?
/// This can be written with `filter` then `map` instead, which would reduce nesting and
/// separates the filtering from the transformation phase. This comes with no cost to
/// performance and is just cleaner.
///
/// ### Limitations
/// Does not lint `bool::then_some`, as it eagerly evaluates its arguments rather than lazily.
/// This can create differing behavior, so better safe than sorry.
///
/// ### Example
/// ```rust
/// # fn really_expensive_fn(i: i32) -> i32 { i }
/// # let v = vec![];
/// _ = v.into_iter().filter_map(|i| (i % 2 == 0).then(|| really_expensive_fn(i)));
/// ```
/// Use instead:
/// ```rust
/// # fn really_expensive_fn(i: i32) -> i32 { i }
/// # let v = vec![];
/// _ = v.into_iter().filter(|i| i % 2 == 0).map(|i| really_expensive_fn(i));
/// ```
#[clippy::version
=
"1.72.0"
]
pub
FILTER_MAP_BOOL_THEN
,
style
,
"checks for usage of `bool::then` in `Iterator::filter_map`"
}
declare_clippy_lint!
{
/// ### What it does
/// Looks for calls to `RwLock::write` where the lock is only used for reading.
...
...
@@ -3676,7 +3644,6 @@ pub fn new(
FORMAT_COLLECT
,
STRING_LIT_CHARS_ANY
,
ITER_SKIP_ZERO
,
FILTER_MAP_BOOL_THEN
,
READONLY_WRITE_LOCK
]);
...
...
@@ -3955,7 +3922,6 @@ fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
},
(
"filter_map"
,
[
arg
])
=>
{
unnecessary_filter_map
::
check
(
cx
,
expr
,
arg
,
name
);
filter_map_bool_then
::
check
(
cx
,
expr
,
arg
,
call_span
);
filter_map_identity
::
check
(
cx
,
expr
,
arg
,
span
);
},
(
"find_map"
,
[
arg
])
=>
{
...
...
src/tools/clippy/clippy_utils/src/paths.rs
浏览文件 @
c0ae75bf
...
...
@@ -163,5 +163,3 @@
pub
const
FORMATTER
:
[
&
str
;
3
]
=
[
"core"
,
"fmt"
,
"Formatter"
];
pub
const
DEBUG_STRUCT
:
[
&
str
;
4
]
=
[
"core"
,
"fmt"
,
"builders"
,
"DebugStruct"
];
pub
const
ORD_CMP
:
[
&
str
;
4
]
=
[
"core"
,
"cmp"
,
"Ord"
,
"cmp"
];
#[expect(clippy::invalid_paths)]
// not sure why it thinks this, it works so
pub
const
BOOL_THEN
:
[
&
str
;
4
]
=
[
"core"
,
"bool"
,
"<impl bool>"
,
"then"
];
src/tools/clippy/tests/ui/filter_map_bool_then.fixed
已删除
100644 → 0
浏览文件 @
e3590fcc
//@run-rustfix
//@aux-build:proc_macros.rs:proc-macro
#![allow(
clippy::clone_on_copy,
clippy::map_identity,
clippy::unnecessary_lazy_evaluations,
unused
)]
#![warn(clippy::filter_map_bool_then)]
#[macro_use]
extern crate proc_macros;
#[derive(Clone, PartialEq)]
struct NonCopy;
fn main() {
let v = vec![1, 2, 3, 4, 5, 6];
v.clone().iter().filter(|&i| (i % 2 == 0)).map(|i| i + 1);
v.clone().into_iter().filter(|&i| (i % 2 == 0)).map(|i| i + 1);
v.clone()
.into_iter()
.filter(|&i| (i % 2 == 0)).map(|i| i + 1);
v.clone()
.into_iter()
.filter(|&i| i != 1000)
.filter(|&i| (i % 2 == 0)).map(|i| i + 1);
v.iter()
.copied()
.filter(|&i| i != 1000)
.filter(|&i| (i.clone() % 2 == 0)).map(|i| i + 1);
// Do not lint
let v = vec![NonCopy, NonCopy];
v.clone().iter().filter_map(|i| (i == &NonCopy).then(|| i));
external! {
let v = vec![1, 2, 3, 4, 5, 6];
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
}
with_span! {
span
let v = vec![1, 2, 3, 4, 5, 6];
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
}
}
src/tools/clippy/tests/ui/filter_map_bool_then.rs
已删除
100644 → 0
浏览文件 @
e3590fcc
//@run-rustfix
//@aux-build:proc_macros.rs:proc-macro
#![allow(
clippy::clone_on_copy,
clippy::map_identity,
clippy::unnecessary_lazy_evaluations,
unused
)]
#![warn(clippy::filter_map_bool_then)]
#[macro_use]
extern
crate
proc_macros
;
#[derive(Clone,
PartialEq)]
struct
NonCopy
;
fn
main
()
{
let
v
=
vec!
[
1
,
2
,
3
,
4
,
5
,
6
];
v
.clone
()
.iter
()
.filter_map
(|
i
|
(
i
%
2
==
0
)
.then
(||
i
+
1
));
v
.clone
()
.into_iter
()
.filter_map
(|
i
|
(
i
%
2
==
0
)
.then
(||
i
+
1
));
v
.clone
()
.into_iter
()
.filter_map
(|
i
|
->
Option
<
_
>
{
(
i
%
2
==
0
)
.then
(||
i
+
1
)
});
v
.clone
()
.into_iter
()
.filter
(|
&
i
|
i
!=
1000
)
.filter_map
(|
i
|
(
i
%
2
==
0
)
.then
(||
i
+
1
));
v
.iter
()
.copied
()
.filter
(|
&
i
|
i
!=
1000
)
.filter_map
(|
i
|
(
i
.clone
()
%
2
==
0
)
.then
(||
i
+
1
));
// Do not lint
let
v
=
vec!
[
NonCopy
,
NonCopy
];
v
.clone
()
.iter
()
.filter_map
(|
i
|
(
i
==
&
NonCopy
)
.then
(||
i
));
external!
{
let
v
=
vec!
[
1
,
2
,
3
,
4
,
5
,
6
];
v
.clone
()
.into_iter
()
.filter_map
(|
i
|
(
i
%
2
==
0
)
.then
(||
i
+
1
));
}
with_span!
{
span
let
v
=
vec!
[
1
,
2
,
3
,
4
,
5
,
6
];
v
.clone
()
.into_iter
()
.filter_map
(|
i
|
(
i
%
2
==
0
)
.then
(||
i
+
1
));
}
}
src/tools/clippy/tests/ui/filter_map_bool_then.stderr
已删除
100644 → 0
浏览文件 @
e3590fcc
error: usage of `bool::then` in `filter_map`
--> $DIR/filter_map_bool_then.rs:19:22
|
LL | v.clone().iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
|
= note: `-D clippy::filter-map-bool-then` implied by `-D warnings`
error: usage of `bool::then` in `filter_map`
--> $DIR/filter_map_bool_then.rs:20:27
|
LL | v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
error: usage of `bool::then` in `filter_map`
--> $DIR/filter_map_bool_then.rs:23:10
|
LL | .filter_map(|i| -> Option<_> { (i % 2 == 0).then(|| i + 1) });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
error: usage of `bool::then` in `filter_map`
--> $DIR/filter_map_bool_then.rs:27:10
|
LL | .filter_map(|i| (i % 2 == 0).then(|| i + 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
error: usage of `bool::then` in `filter_map`
--> $DIR/filter_map_bool_then.rs:31:10
|
LL | .filter_map(|i| (i.clone() % 2 == 0).then(|| i + 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i.clone() % 2 == 0)).map(|i| i + 1)`
error: aborting due to 5 previous errors
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录