Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Clippy #76574

Merged
merged 80 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
2ecc2ac
unit-arg - improve suggestion
tnielens Aug 20, 2020
baf62e7
Update changelog to beta 1.47
ebroto Aug 27, 2020
f3ccbef
unit-arg - pr comments
tnielens Aug 26, 2020
5574182
Add a new lint to prevent `create_dir` from being used
hkmatsumoto Aug 28, 2020
607905d
Add STD_FS_CREATE_DIR into paths
hkmatsumoto Aug 28, 2020
34e302e
Fix clippy error
hkmatsumoto Aug 28, 2020
eebd248
Fix errors
hkmatsumoto Aug 28, 2020
c88c614
Auto merge of #5970 - ebroto:changelog_beta_1_47, r=flip1995
bors Aug 28, 2020
282c598
Merge commit '3d0b0e66afdfaa519d8855b338b35b4605775945' into clippyup
flip1995 Aug 28, 2020
7a66e65
or_fn_call: ignore nullary associated const fns
ebroto Aug 28, 2020
5b7590f
Downgrade applicability of `create_dir`
hkmatsumoto Aug 29, 2020
4972989
Add a lint for an async block/closure that yields a type that is itse…
khuey Aug 15, 2020
c1d2b93
Add a test for an async function.
khuey Aug 23, 2020
04912ca
Formatting changes requested by ThibsG.
khuey Aug 23, 2020
1c5b0fb
Update dataflow analyses to use new interface
ecstatic-morse Aug 28, 2020
17b2ba5
Syntax-highlight `single_char_push_str` lint
camelid Aug 30, 2020
ab64d47
Auto merge of #5988 - camelid:patch-2, r=ebroto
bors Aug 30, 2020
fdc48fb
Fix clippy
Aaron1011 Aug 30, 2020
001c1c5
Auto merge of #5984 - ebroto:5693_const_assoc_fn, r=flip1995
bors Aug 31, 2020
451ef78
Use `match_def_path` instead of `match_qpath`
hkmatsumoto Aug 31, 2020
001f9e4
Fix the wrong suggestion when using macro in `collapsible_if`
giraffate Aug 31, 2020
8b0aa6a
default_trait_access: Fix wrong suggestion
taiki-e Aug 31, 2020
8334a58
Auto merge of #5909 - khuey:async_yields_async, r=yaahc
bors Aug 31, 2020
066f105
Auto merge of #5992 - giraffate:fix_wrong_seggestion_in_collapsible_i…
bors Aug 31, 2020
afeb917
Fix a fp in `transmute_ptr_to_ptr`
rail-rain Aug 31, 2020
2e4b4ce
useless_attribute: Permit wildcard_imports and enum_glob_use
taiki-e Sep 1, 2020
b304221
Allow GraphQL in doc without backticks
Kestrer Sep 1, 2020
aa7ffa5
Fix FP in `same_item_push`
giraffate Sep 1, 2020
67e18c2
Auto merge of #5993 - taiki-e:default_trait_access, r=phansch
bors Sep 1, 2020
48248af
Auto merge of #5999 - rail-rain:fix_fp_transmute_ptr_to_ptr_in_consts…
bors Sep 1, 2020
da86067
Merge remote-tracking branch 'upstream/master' into sync-from-rust
ebroto Sep 2, 2020
a5754a1
Run cargo dev fmt
ebroto Sep 2, 2020
a62bab2
Auto merge of #6000 - ebroto:sync-from-rust, r=ebroto
bors Sep 2, 2020
246f1f8
Improve recovery on malformed format call
scrabsha Aug 31, 2020
22c9940
Add tests for allowed non-backticked identifiers in doc
Kestrer Sep 2, 2020
7f27b12
Auto merge of #5996 - Koxiaet:master, r=ebroto
bors Sep 2, 2020
b1f0e01
Merge branch 'master' into unit-arg
tnielens Sep 2, 2020
365b13c
Auto merge of #76160 - scileo:format-recovery, r=petrochenkov
bors Sep 2, 2020
b220ddf
unit-arg - pr remarks
tnielens Sep 2, 2020
93ce686
Update ui stderr with improved rustc output
Sep 3, 2020
a31216b
Auto merge of #6003 - mikerite:fix-tests-20200903, r=phansch
bors Sep 3, 2020
8829214
Auto merge of #5994 - taiki-e:useless_attribute, r=phansch
bors Sep 3, 2020
cf1cc7c
Simplify `clippy::default_trait_access`
Sep 4, 2020
b9e35df
Auto merge of #6004 - mikerite:default-trait-access-20200904, r=ebroto
bors Sep 4, 2020
28f9b84
`ty.kind` -> `ty.kind()` in rustdoc and clippy
LeSeulArtichaut Aug 3, 2020
4286d9c
ty.flags -> ty.flags()
LeSeulArtichaut Aug 6, 2020
23646e6
Merge remote-tracking branch 'upstream/master' into sync-from-rust
ebroto Sep 4, 2020
7bcf40a
Fix fallout from rustup
ebroto Sep 4, 2020
2905fff
Run cargo dev fmt
ebroto Sep 4, 2020
f253dd0
Auto merge of #6007 - ebroto:sync-from-rust, r=ebroto
bors Sep 4, 2020
04ba07b
add line_count and max_lines to too_many_lines lint message
Ryan1729 Sep 5, 2020
db16fa2
run tests/ui/update-all-references.sh
Ryan1729 Sep 5, 2020
9e7ce9d
run the specific script suggested by the error message
Ryan1729 Sep 5, 2020
3cecdc9
Auto merge of #6009 - Ryan1729:show-line_count-and-max_lines-in-too_m…
bors Sep 5, 2020
96b31a5
Fix FP when coercion kicks in
giraffate Sep 5, 2020
e9440cb
Auto merge of #5997 - giraffate:fix_fp_about_clone_in_same_item_push,…
bors Sep 5, 2020
9fa9208
Restrict unnecessary_sort_by to non-ref copy types
ebroto Sep 4, 2020
daad592
Auto merge of #6006 - ebroto:6001_unnecessary_sort_by, r=Manishearth
bors Sep 6, 2020
3d30ef7
Restrict `same_item_push` to suppress false positives
giraffate Sep 7, 2020
3ffe9f8
Auto merge of #76044 - ecstatic-morse:dataflow-lattice, r=oli-obk
bors Sep 7, 2020
619ca76
Refactoring: use inner function
giraffate Sep 7, 2020
5d085ad
Some refactoring
giraffate Sep 7, 2020
2c9f82e
Add some tests to `same_item_push`
giraffate Sep 8, 2020
952c046
Refactoring: tests in `same_item_push`
giraffate Sep 8, 2020
1d8ae3a
Address `items_after_statement`
giraffate Sep 8, 2020
a899ad2
Change suggestion to `create_dir_all({})` from `std::fs::create_dir_a…
hkmatsumoto Sep 8, 2020
e83190b
Merge remote-tracking branch 'upstream/master' into rustup
ebroto Sep 8, 2020
8d6cf3a
Auto merge of #6018 - ebroto:rustup, r=ebroto
bors Sep 8, 2020
8b54f1e
Auto merge of #6016 - giraffate:restrict_same_item_push, r=ebroto
bors Sep 8, 2020
f23670e
Adjust Clippy for CONST_ITEM_MUTATION lint
Aaron1011 Sep 8, 2020
4db1029
link to the Box docs in related lint documentation.
matthiaskrgr Sep 9, 2020
de195f2
print the unit type `()` in related lint messages.
matthiaskrgr Sep 9, 2020
961f183
Auto merge of #75573 - Aaron1011:feature/const-mutation-lint, r=oli-obk
bors Sep 10, 2020
e0f46a1
Auto merge of #6024 - matthiaskrgr:unit_type, r=phansch
bors Sep 10, 2020
2d56512
Cleanup of rustup
flip1995 Sep 10, 2020
87a4495
Auto merge of #6027 - flip1995:rustup, r=flip1995
bors Sep 10, 2020
0e9769e
Auto merge of #6023 - matthiaskrgr:box, r=flip1995
bors Sep 10, 2020
55efa96
Auto merge of #5931 - montrivo:unit-arg, r=flip1995
bors Sep 10, 2020
5034d47
Auto merge of #5980 - matsujika:create-dir, r=flip1995
bors Sep 10, 2020
ca6c695
Merge commit '5034d47f721ff4c3a3ff2aca9ef2ef3e1d067f9f' into clippyup
flip1995 Sep 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 107 additions & 3 deletions src/tools/clippy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,113 @@ document.

## Unreleased / In Rust Nightly

[c2c07fa...master](https://github.com/rust-lang/rust-clippy/compare/c2c07fa...master)
[09bd400...master](https://github.com/rust-lang/rust-clippy/compare/09bd400...master)

## Rust 1.47

Current beta, release 2020-10-08

[c2c07fa...09bd400](https://github.com/rust-lang/rust-clippy/compare/c2c07fa...09bd400)

### New lints

* [`derive_ord_xor_partial_ord`] [#5848](https://github.com/rust-lang/rust-clippy/pull/5848)
* [`trait_duplication_in_bounds`] [#5852](https://github.com/rust-lang/rust-clippy/pull/5852)
* [`map_identity`] [#5694](https://github.com/rust-lang/rust-clippy/pull/5694)
* [`unit_return_expecting_ord`] [#5737](https://github.com/rust-lang/rust-clippy/pull/5737)
* [`pattern_type_mismatch`] [#4841](https://github.com/rust-lang/rust-clippy/pull/4841)
* [`repeat_once`] [#5773](https://github.com/rust-lang/rust-clippy/pull/5773)
* [`same_item_push`] [#5825](https://github.com/rust-lang/rust-clippy/pull/5825)
* [`needless_arbitrary_self_type`] [#5869](https://github.com/rust-lang/rust-clippy/pull/5869)
* [`match_like_matches_macro`] [#5769](https://github.com/rust-lang/rust-clippy/pull/5769)
* [`stable_sort_primitive`] [#5809](https://github.com/rust-lang/rust-clippy/pull/5809)
* [`blanket_clippy_restriction_lints`] [#5750](https://github.com/rust-lang/rust-clippy/pull/5750)
* [`option_if_let_else`] [#5301](https://github.com/rust-lang/rust-clippy/pull/5301)

### Moves and Deprecations

* Deprecate [`regex_macro`] lint
[#5760](https://github.com/rust-lang/rust-clippy/pull/5760)
* Move [`range_minus_one`] to `pedantic`
[#5752](https://github.com/rust-lang/rust-clippy/pull/5752)

### Enhancements

* Improve [`needless_collect`] by catching `collect` calls followed by `iter` or `into_iter` calls
[#5837](https://github.com/rust-lang/rust-clippy/pull/5837)
* [`panic`], [`todo`], [`unimplemented`] and [`unreachable`] now detect calls with formatting
[#5811](https://github.com/rust-lang/rust-clippy/pull/5811)
* Detect more cases of [`suboptimal_flops`] and [`imprecise_flops`]
[#5443](https://github.com/rust-lang/rust-clippy/pull/5443)
* Handle asymmetrical implementations of `PartialEq` in [`cmp_owned`]
[#5701](https://github.com/rust-lang/rust-clippy/pull/5701)
* Make it possible to allow [`unsafe_derive_deserialize`]
[#5870](https://github.com/rust-lang/rust-clippy/pull/5870)
* Catch `ord.min(a).max(b)` where a < b in [`min_max`]
[#5871](https://github.com/rust-lang/rust-clippy/pull/5871)
* Make [`clone_on_copy`] suggestion machine applicable
[#5745](https://github.com/rust-lang/rust-clippy/pull/5745)
* Enable [`len_zero`] on ranges now that `is_empty` is stable on them
[#5961](https://github.com/rust-lang/rust-clippy/pull/5961)

### False Positive Fixes

* Avoid triggering [`or_fun_call`] with const fns that take no arguments
[#5889](https://github.com/rust-lang/rust-clippy/pull/5889)
* Fix [`redundant_closure_call`] false positive for closures that have multiple calls
[#5800](https://github.com/rust-lang/rust-clippy/pull/5800)
* Don't lint cases involving `ManuallyDrop` in [`redundant_clone`]
[#5824](https://github.com/rust-lang/rust-clippy/pull/5824)
* Treat a single expression the same as a single statement in the 2nd arm of a match in [`single_match_else`]
[#5771](https://github.com/rust-lang/rust-clippy/pull/5771)
* Don't trigger [`unnested_or_patterns`] if the feature `or_patterns` is not enabled
[#5758](https://github.com/rust-lang/rust-clippy/pull/5758)
* Avoid linting if key borrows in [`unnecessary_sort_by`]
[#5756](https://github.com/rust-lang/rust-clippy/pull/5756)
* Consider `Try` impl for `Poll` when generating suggestions in [`try_err`]
[#5857](https://github.com/rust-lang/rust-clippy/pull/5857)
* Take input lifetimes into account in `manual_async_fn`
[#5859](https://github.com/rust-lang/rust-clippy/pull/5859)
* Fix multiple false positives in [`type_repetition_in_bounds`] and add a configuration option
[#5761](https://github.com/rust-lang/rust-clippy/pull/5761)
* Limit the [`suspicious_arithmetic_impl`] lint to one binary operation
[#5820](https://github.com/rust-lang/rust-clippy/pull/5820)

### Suggestion Fixes/Improvements

* Improve readability of [`shadow_unrelated`] suggestion by truncating the RHS snippet
[#5788](https://github.com/rust-lang/rust-clippy/pull/5788)
* Suggest `filter_map` instead of `flat_map` when mapping to `Option` in [`map_flatten`]
[#5846](https://github.com/rust-lang/rust-clippy/pull/5846)
* Ensure suggestion is shown correctly for long method call chains in [`iter_nth_zero`]
[#5793](https://github.com/rust-lang/rust-clippy/pull/5793)
* Drop borrow operator in suggestions of [`redundant_pattern_matching`]
[#5815](https://github.com/rust-lang/rust-clippy/pull/5815)
* Add suggestion for [`iter_skip_next`]
[#5843](https://github.com/rust-lang/rust-clippy/pull/5843)
* Improve [`collapsible_if`] fix suggestion
[#5732](https://github.com/rust-lang/rust-clippy/pull/5732)

### ICE Fixes

* Fix ICE caused by [`needless_collect`]
[#5877](https://github.com/rust-lang/rust-clippy/pull/5877)
* Fix ICE caused by [`unnested_or_patterns`]
[#5784](https://github.com/rust-lang/rust-clippy/pull/5784)

### Documentation Improvements

* Fix grammar of [`await_holding_lock`] documentation
[#5748](https://github.com/rust-lang/rust-clippy/pull/5748)

### Others

* Make lints adhere to the rustc dev guide
[#5888](https://github.com/rust-lang/rust-clippy/pull/5888)

## Rust 1.46

Current beta, release 2020-08-27
Current stable, released 2020-08-27

[7ea7cd1...c2c07fa](https://github.com/rust-lang/rust-clippy/compare/7ea7cd1...c2c07fa)

Expand Down Expand Up @@ -72,7 +174,7 @@ Current beta, release 2020-08-27

## Rust 1.45

Current stable, released 2020-07-16
Released 2020-07-16

[891e1a8...7ea7cd1](https://github.com/rust-lang/rust-clippy/compare/891e1a8...7ea7cd1)

Expand Down Expand Up @@ -1410,6 +1512,7 @@ Released 2018-09-13
[`assertions_on_constants`]: https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants
[`assign_op_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#assign_op_pattern
[`assign_ops`]: https://rust-lang.github.io/rust-clippy/master/index.html#assign_ops
[`async_yields_async`]: https://rust-lang.github.io/rust-clippy/master/index.html#async_yields_async
[`await_holding_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_lock
[`bad_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask
[`bind_instead_of_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#bind_instead_of_map
Expand Down Expand Up @@ -1444,6 +1547,7 @@ Released 2018-09-13
[`collapsible_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
[`comparison_chain`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
[`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator
[`create_dir`]: https://rust-lang.github.io/rust-clippy/master/index.html#create_dir
[`crosspointer_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#crosspointer_transmute
[`dbg_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro
[`debug_assert_with_mut_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#debug_assert_with_mut_call
Expand Down
86 changes: 86 additions & 0 deletions src/tools/clippy/clippy_lints/src/async_yields_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use crate::utils::{implements_trait, snippet, span_lint_and_then};
use rustc_errors::Applicability;
use rustc_hir::{AsyncGeneratorKind, Body, BodyId, ExprKind, GeneratorKind, QPath};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
/// **What it does:** Checks for async blocks that yield values of types
/// that can themselves be awaited.
///
/// **Why is this bad?** An await is likely missing.
///
/// **Known problems:** None.
///
/// **Example:**
///
/// ```rust
/// async fn foo() {}
///
/// fn bar() {
/// let x = async {
/// foo()
/// };
/// }
/// ```
/// Use instead:
/// ```rust
/// async fn foo() {}
///
/// fn bar() {
/// let x = async {
/// foo().await
/// };
/// }
/// ```
pub ASYNC_YIELDS_ASYNC,
correctness,
"async blocks that return a type that can be awaited"
}

declare_lint_pass!(AsyncYieldsAsync => [ASYNC_YIELDS_ASYNC]);

impl<'tcx> LateLintPass<'tcx> for AsyncYieldsAsync {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
use AsyncGeneratorKind::{Block, Closure};
// For functions, with explicitly defined types, don't warn.
// XXXkhuey maybe we should?
if let Some(GeneratorKind::Async(Block | Closure)) = body.generator_kind {
if let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait() {
let body_id = BodyId {
hir_id: body.value.hir_id,
};
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
let typeck_results = cx.tcx.typeck(def_id);
let expr_ty = typeck_results.expr_ty(&body.value);

if implements_trait(cx, expr_ty, future_trait_def_id, &[]) {
let return_expr_span = match &body.value.kind {
// XXXkhuey there has to be a better way.
ExprKind::Block(block, _) => block.expr.map(|e| e.span),
ExprKind::Path(QPath::Resolved(_, path)) => Some(path.span),
_ => None,
};
if let Some(return_expr_span) = return_expr_span {
span_lint_and_then(
cx,
ASYNC_YIELDS_ASYNC,
return_expr_span,
"an async construct yields a type which is itself awaitable",
|db| {
db.span_label(body.value.span, "outer async construct");
db.span_label(return_expr_span, "awaitable value not awaited");
db.span_suggestion(
return_expr_span,
"consider awaiting this value",
format!("{}.await", snippet(cx, return_expr_span, "..")),
Applicability::MaybeIncorrect,
);
},
);
}
}
}
}
}
}
38 changes: 22 additions & 16 deletions src/tools/clippy/clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ declare_clippy_lint! {
/// **What it does:** Checks for `extern crate` and `use` items annotated with
/// lint attributes.
///
/// This lint permits `#[allow(unused_imports)]`, `#[allow(deprecated)]` and
/// `#[allow(unreachable_pub)]` on `use` items and `#[allow(unused_imports)]` on
/// This lint permits `#[allow(unused_imports)]`, `#[allow(deprecated)]`,
/// `#[allow(unreachable_pub)]`, `#[allow(clippy::wildcard_imports)]` and
/// `#[allow(clippy::enum_glob_use)]` on `use` items and `#[allow(unused_imports)]` on
/// `extern crate` items with a `#[macro_use]` attribute.
///
/// **Why is this bad?** Lint attributes have no effect on crate imports. Most
Expand Down Expand Up @@ -318,7 +319,8 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
if let Some(ident) = attr.ident() {
match &*ident.as_str() {
"allow" | "warn" | "deny" | "forbid" => {
// permit `unused_imports`, `deprecated` and `unreachable_pub` for `use` items
// permit `unused_imports`, `deprecated`, `unreachable_pub`,
// `clippy::wildcard_imports`, and `clippy::enum_glob_use` for `use` items
// and `unused_imports` for `extern crate` items with `macro_use`
for lint in lint_list {
match item.kind {
Expand All @@ -327,6 +329,9 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
|| is_word(lint, sym!(deprecated))
|| is_word(lint, sym!(unreachable_pub))
|| is_word(lint, sym!(unused))
|| extract_clippy_lint(lint)
.map_or(false, |s| s == "wildcard_imports")
|| extract_clippy_lint(lint).map_or(false, |s| s == "enum_glob_use")
{
return;
}
Expand Down Expand Up @@ -387,24 +392,25 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
}
}

fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMetaItem]) {
fn extract_name(lint: &NestedMetaItem) -> Option<SymbolStr> {
if_chain! {
if let Some(meta_item) = lint.meta_item();
if meta_item.path.segments.len() > 1;
if let tool_name = meta_item.path.segments[0].ident;
if tool_name.as_str() == "clippy";
let lint_name = meta_item.path.segments.last().unwrap().ident.name;
then {
return Some(lint_name.as_str());
}
/// Returns the lint name if it is clippy lint.
fn extract_clippy_lint(lint: &NestedMetaItem) -> Option<SymbolStr> {
if_chain! {
if let Some(meta_item) = lint.meta_item();
if meta_item.path.segments.len() > 1;
if let tool_name = meta_item.path.segments[0].ident;
if tool_name.as_str() == "clippy";
let lint_name = meta_item.path.segments.last().unwrap().ident.name;
then {
return Some(lint_name.as_str());
}
None
}
None
}

fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMetaItem]) {
let lint_store = cx.lints();
for lint in items {
if let Some(lint_name) = extract_name(lint) {
if let Some(lint_name) = extract_clippy_lint(lint) {
if let CheckLintNameResult::Tool(Err((None, _))) =
lint_store.check_lint_name(&lint_name, Some(sym!(clippy)))
{
Expand Down
51 changes: 51 additions & 0 deletions src/tools/clippy/clippy_lints/src/create_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::utils::{match_def_path, paths, snippet, span_lint_and_sugg};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
/// **What it does:** Checks usage of `std::fs::create_dir` and suggest using `std::fs::create_dir_all` instead.
///
/// **Why is this bad?** Sometimes `std::fs::crate_dir` is mistakenly chosen over `std::fs::create_dir_all`.
///
/// **Known problems:** None.
///
/// **Example:**
///
/// ```rust
/// std::fs::create_dir("foo");
/// ```
/// Use instead:
/// ```rust
/// std::fs::create_dir_all("foo");
/// ```
pub CREATE_DIR,
restriction,
"calling `std::fs::create_dir` instead of `std::fs::create_dir_all`"
}

declare_lint_pass!(CreateDir => [CREATE_DIR]);

impl LateLintPass<'_> for CreateDir {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
if_chain! {
if let ExprKind::Call(ref func, ref args) = expr.kind;
if let ExprKind::Path(ref path) = func.kind;
if let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id();
if match_def_path(cx, def_id, &paths::STD_FS_CREATE_DIR);
then {
span_lint_and_sugg(
cx,
CREATE_DIR,
expr.span,
"calling `std::fs::create_dir` where there may be a better way",
"consider calling `std::fs::create_dir_all` instead",
format!("create_dir_all({})", snippet(cx, args[0].span, "..")),
Applicability::MaybeIncorrect,
)
}
}
}
}
Loading