Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit e988498

Browse files
committed
Merge branch 'master' into hacspec
2 parents 7037544 + 100a24d commit e988498

24 files changed

+556
-125
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,7 @@ Released 2018-09-13
14391439
[`redundant_field_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names
14401440
[`redundant_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern
14411441
[`redundant_pattern_matching`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching
1442+
[`redundant_pub_crate`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pub_crate
14421443
[`redundant_static_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes
14431444
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
14441445
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
77

8-
[There are 361 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
8+
[There are 362 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
99

1010
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1111

clippy_lints/src/doc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ fn lint_for_missing_headers<'a, 'tcx>(
234234
if implements_trait(cx, ret_ty, future, &[]);
235235
if let ty::Opaque(_, subs) = ret_ty.kind;
236236
if let Some(gen) = subs.types().next();
237-
if let ty::Generator(def_id, subs, _) = gen.kind;
238-
if match_type(cx, subs.as_generator().return_ty(def_id, cx.tcx), &paths::RESULT);
237+
if let ty::Generator(_, subs, _) = gen.kind;
238+
if match_type(cx, subs.as_generator().return_ty(), &paths::RESULT);
239239
then {
240240
span_lint(
241241
cx,

clippy_lints/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ pub mod ranges;
286286
pub mod redundant_clone;
287287
pub mod redundant_field_names;
288288
pub mod redundant_pattern_matching;
289+
pub mod redundant_pub_crate;
289290
pub mod redundant_static_lifetimes;
290291
pub mod reference;
291292
pub mod regex;
@@ -324,7 +325,7 @@ pub mod zero_div_zero;
324325
pub use crate::utils::conf::Conf;
325326

326327
mod reexport {
327-
crate use rustc_ast::ast::Name;
328+
pub use rustc_ast::ast::Name;
328329
}
329330

330331
/// Register all pre expansion lints
@@ -747,6 +748,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
747748
&redundant_clone::REDUNDANT_CLONE,
748749
&redundant_field_names::REDUNDANT_FIELD_NAMES,
749750
&redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING,
751+
&redundant_pub_crate::REDUNDANT_PUB_CRATE,
750752
&redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES,
751753
&reference::DEREF_ADDROF,
752754
&reference::REF_IN_DEREF,
@@ -1025,6 +1027,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10251027
store.register_late_pass(|| box wildcard_imports::WildcardImports);
10261028
store.register_early_pass(|| box macro_use::MacroUseImports);
10271029
store.register_late_pass(|| box verbose_file_reads::VerboseFileReads);
1030+
store.register_late_pass(|| box redundant_pub_crate::RedundantPubCrate::default());
10281031

10291032
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
10301033
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
@@ -1326,6 +1329,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13261329
LintId::of(&redundant_clone::REDUNDANT_CLONE),
13271330
LintId::of(&redundant_field_names::REDUNDANT_FIELD_NAMES),
13281331
LintId::of(&redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING),
1332+
LintId::of(&redundant_pub_crate::REDUNDANT_PUB_CRATE),
13291333
LintId::of(&redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES),
13301334
LintId::of(&reference::DEREF_ADDROF),
13311335
LintId::of(&reference::REF_IN_DEREF),
@@ -1355,7 +1359,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13551359
LintId::of(&transmute::TRANSMUTE_PTR_TO_PTR),
13561360
LintId::of(&transmute::TRANSMUTE_PTR_TO_REF),
13571361
LintId::of(&transmute::UNSOUND_COLLECTION_TRANSMUTE),
1358-
LintId::of(&transmute::USELESS_TRANSMUTE),
13591362
LintId::of(&transmute::WRONG_TRANSMUTE),
13601363
LintId::of(&transmuting_null::TRANSMUTING_NULL),
13611364
LintId::of(&trivially_copy_pass_by_ref::TRIVIALLY_COPY_PASS_BY_REF),
@@ -1467,6 +1470,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14671470
LintId::of(&question_mark::QUESTION_MARK),
14681471
LintId::of(&redundant_field_names::REDUNDANT_FIELD_NAMES),
14691472
LintId::of(&redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING),
1473+
LintId::of(&redundant_pub_crate::REDUNDANT_PUB_CRATE),
14701474
LintId::of(&redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES),
14711475
LintId::of(&regex::REGEX_MACRO),
14721476
LintId::of(&regex::TRIVIAL_REGEX),
@@ -1557,7 +1561,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15571561
LintId::of(&transmute::TRANSMUTE_INT_TO_FLOAT),
15581562
LintId::of(&transmute::TRANSMUTE_PTR_TO_PTR),
15591563
LintId::of(&transmute::TRANSMUTE_PTR_TO_REF),
1560-
LintId::of(&transmute::USELESS_TRANSMUTE),
15611564
LintId::of(&types::BORROWED_BOX),
15621565
LintId::of(&types::CHAR_LIT_AS_U8),
15631566
LintId::of(&types::OPTION_OPTION),
@@ -1674,6 +1677,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16741677
LintId::of(&mutex_atomic::MUTEX_INTEGER),
16751678
LintId::of(&needless_borrow::NEEDLESS_BORROW),
16761679
LintId::of(&path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE),
1680+
LintId::of(&transmute::USELESS_TRANSMUTE),
16771681
LintId::of(&use_self::USE_SELF),
16781682
]);
16791683
}

clippy_lints/src/lifetimes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn check_fn_inner<'a, 'tcx>(
169169
span_lint(
170170
cx,
171171
NEEDLESS_LIFETIMES,
172-
span,
172+
span.with_hi(decl.output.span().hi()),
173173
"explicit lifetimes given in parameter types where they could be elided \
174174
(or replaced with `'_` if needed by type declaration)",
175175
);

clippy_lints/src/matches.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use crate::utils::paths;
33
use crate::utils::sugg::Sugg;
44
use crate::utils::usage::is_unused;
55
use crate::utils::{
6-
expr_block, get_arg_name, in_macro, indent_of, is_allowed, is_expn_of, is_refutable, is_wild, match_qpath,
7-
match_type, match_var, multispan_sugg, remove_blocks, snippet, snippet_block, snippet_with_applicability,
8-
span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then, walk_ptrs_ty,
6+
expr_block, get_arg_name, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of, is_refutable, is_wild,
7+
match_qpath, match_type, match_var, multispan_sugg, remove_blocks, snippet, snippet_block,
8+
snippet_with_applicability, span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then,
9+
walk_ptrs_ty,
910
};
1011
use if_chain::if_chain;
1112
use rustc::lint::in_external_macro;
@@ -928,14 +929,27 @@ fn check_match_single_binding<'a>(cx: &LateContext<'_, 'a>, ex: &Expr<'a>, arms:
928929
),
929930
)
930931
} else {
932+
// If we are in closure, we need curly braces around suggestion
933+
let mut indent = " ".repeat(indent_of(cx, ex.span).unwrap_or(0));
934+
let (mut cbrace_start, mut cbrace_end) = ("".to_string(), "".to_string());
935+
if let Some(parent_expr) = get_parent_expr(cx, expr) {
936+
if let ExprKind::Closure(..) = parent_expr.kind {
937+
cbrace_end = format!("\n{}}}", indent);
938+
// Fix body indent due to the closure
939+
indent = " ".repeat(indent_of(cx, bind_names).unwrap_or(0));
940+
cbrace_start = format!("{{\n{}", indent);
941+
}
942+
};
931943
(
932944
expr.span,
933945
format!(
934-
"let {} = {};\n{}{}",
946+
"{}let {} = {};\n{}{}{}",
947+
cbrace_start,
935948
snippet_with_applicability(cx, bind_names, "..", &mut applicability),
936949
snippet_with_applicability(cx, matched_vars, "..", &mut applicability),
937-
" ".repeat(indent_of(cx, expr.span).unwrap_or(0)),
938-
snippet_body
950+
indent,
951+
snippet_body,
952+
cbrace_end
939953
),
940954
)
941955
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use crate::utils::span_lint_and_then;
2+
use rustc_errors::Applicability;
3+
use rustc_hir::{Item, ItemKind, VisibilityKind};
4+
use rustc_lint::{LateContext, LateLintPass};
5+
use rustc_session::{declare_tool_lint, impl_lint_pass};
6+
7+
declare_clippy_lint! {
8+
/// **What it does:** Checks for items declared `pub(crate)` that are not crate visible because they
9+
/// are inside a private module.
10+
///
11+
/// **Why is this bad?** Writing `pub(crate)` is misleading when it's redundant due to the parent
12+
/// module's visibility.
13+
///
14+
/// **Known problems:** None.
15+
///
16+
/// **Example:**
17+
///
18+
/// ```rust
19+
/// mod internal {
20+
/// pub(crate) fn internal_fn() { }
21+
/// }
22+
/// ```
23+
/// This function is not visible outside the module and it can be declared with `pub` or
24+
/// private visibility
25+
/// ```rust
26+
/// mod internal {
27+
/// pub fn internal_fn() { }
28+
/// }
29+
/// ```
30+
pub REDUNDANT_PUB_CRATE,
31+
style,
32+
"Using `pub(crate)` visibility on items that are not crate visible due to the visibility of the module that contains them."
33+
}
34+
35+
#[derive(Default)]
36+
pub struct RedundantPubCrate {
37+
is_exported: Vec<bool>,
38+
}
39+
40+
impl_lint_pass!(RedundantPubCrate => [REDUNDANT_PUB_CRATE]);
41+
42+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantPubCrate {
43+
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'tcx>) {
44+
if let VisibilityKind::Crate { .. } = item.vis.node {
45+
if !cx.access_levels.is_exported(item.hir_id) {
46+
if let Some(false) = self.is_exported.last() {
47+
let span = item.span.with_hi(item.ident.span.hi());
48+
span_lint_and_then(
49+
cx,
50+
REDUNDANT_PUB_CRATE,
51+
span,
52+
&format!("pub(crate) {} inside private module", item.kind.descr()),
53+
|db| {
54+
db.span_suggestion(
55+
item.vis.span,
56+
"consider using",
57+
"pub".to_string(),
58+
Applicability::MachineApplicable,
59+
);
60+
},
61+
)
62+
}
63+
}
64+
}
65+
66+
if let ItemKind::Mod { .. } = item.kind {
67+
self.is_exported.push(cx.access_levels.is_exported(item.hir_id));
68+
}
69+
}
70+
71+
fn check_item_post(&mut self, _cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'tcx>) {
72+
if let ItemKind::Mod { .. } = item.kind {
73+
self.is_exported.pop().expect("unbalanced check_item/check_item_post");
74+
}
75+
}
76+
}

clippy_lints/src/transmute.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ declare_clippy_lint! {
2828
"transmutes that are confusing at best, undefined behaviour at worst and always useless"
2929
}
3030

31+
// FIXME: Move this to `complexity` again, after #5343 is fixed
3132
declare_clippy_lint! {
3233
/// **What it does:** Checks for transmutes to the original type of the object
3334
/// and transmutes that could be a cast.
@@ -42,7 +43,7 @@ declare_clippy_lint! {
4243
/// core::intrinsics::transmute(t); // where the result type is the same as `t`'s
4344
/// ```
4445
pub USELESS_TRANSMUTE,
45-
complexity,
46+
nursery,
4647
"transmutes that have the same to and from types or could be a cast/coercion"
4748
}
4849

clippy_lints/src/utils/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ macro_rules! define_Conf {
8080
$(
8181
mod $config {
8282
use serde::Deserialize;
83-
crate fn deserialize<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<$Ty, D::Error> {
83+
pub fn deserialize<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<$Ty, D::Error> {
8484
use super::super::{ERRORS, Error};
8585
Ok(
8686
<$Ty>::deserialize(deserializer).unwrap_or_else(|e| {

clippy_lints/src/utils/numeric_literal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast::ast::{Lit, LitFloatType, LitIntType, LitKind};
22

33
#[derive(Debug, PartialEq)]
4-
pub(crate) enum Radix {
4+
pub enum Radix {
55
Binary,
66
Octal,
77
Decimal,
@@ -26,7 +26,7 @@ pub fn format(lit: &str, type_suffix: Option<&str>, float: bool) -> String {
2626
}
2727

2828
#[derive(Debug)]
29-
pub(crate) struct NumericLiteral<'a> {
29+
pub struct NumericLiteral<'a> {
3030
/// Which radix the literal was represented in.
3131
pub radix: Radix,
3232
/// The radix prefix, if present.

0 commit comments

Comments
 (0)