Skip to content

Commit c2ee9c2

Browse files
author
Michael Wright
committed
Merge branch 'master' into unnecessary_filter_map
2 parents 06f6b36 + 67d85bc commit c2ee9c2

File tree

11 files changed

+51
-249
lines changed

11 files changed

+51
-249
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ out
1515
*.exe
1616

1717
# Generated by Cargo
18-
Cargo.lock
18+
*Cargo.lock
1919
/target
2020
/clippy_lints/target
2121
/clippy_workspace_tests/target

clippy_dev/Cargo.lock

Lines changed: 0 additions & 224 deletions
This file was deleted.

clippy_dev/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ pub struct Lint {
3535
}
3636

3737
impl Lint {
38-
pub fn new(name: &str, group: &str, desc: &str, deprecation: Option<&str>, module: &str) -> Lint {
39-
Lint {
38+
pub fn new(name: &str, group: &str, desc: &str, deprecation: Option<&str>, module: &str) -> Self {
39+
Self {
4040
name: name.to_lowercase(),
4141
group: group.to_string(),
4242
desc: NL_ESCAPE_RE.replace(&desc.replace("\\\"", "\""), "").to_string(),
@@ -46,12 +46,12 @@ impl Lint {
4646
}
4747

4848
/// Returns all non-deprecated lints
49-
pub fn active_lints(lints: &[Lint]) -> impl Iterator<Item=&Lint> {
49+
pub fn active_lints(lints: &[Self]) -> impl Iterator<Item=&Self> {
5050
lints.iter().filter(|l| l.deprecation.is_none())
5151
}
5252

5353
/// Returns the lints in a HashMap, grouped by the different lint groups
54-
pub fn by_lint_group(lints: &[Lint]) -> HashMap<String, Vec<Lint>> {
54+
pub fn by_lint_group(lints: &[Self]) -> HashMap<String, Vec<Self>> {
5555
lints.iter().map(|lint| (lint.group.to_string(), lint.clone())).into_group_map()
5656
}
5757
}

clippy_lints/src/consts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ impl Constant {
123123
(&Constant::Tuple(ref l), &Constant::Tuple(ref r)) | (&Constant::Vec(ref l), &Constant::Vec(ref r)) => l
124124
.iter()
125125
.zip(r.iter())
126-
.map(|(li, ri)| Constant::partial_cmp(tcx, cmp_type, li, ri))
126+
.map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
127127
.find(|r| r.map_or(true, |o| o != Ordering::Equal))
128128
.unwrap_or_else(|| Some(l.len().cmp(&r.len()))),
129129
(&Constant::Repeat(ref lv, ref ls), &Constant::Repeat(ref rv, ref rs)) => {
130-
match Constant::partial_cmp(tcx, cmp_type, lv, rv) {
130+
match Self::partial_cmp(tcx, cmp_type, lv, rv) {
131131
Some(Equal) => Some(ls.cmp(rs)),
132132
x => x,
133133
}

clippy_lints/src/duration_subsec.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DurationSubsec {
4646
if let Some((Constant::Int(divisor), _)) = constant(cx, cx.tables, right);
4747
then {
4848
let suggested_fn = match (method_path.ident.as_str().as_ref(), divisor) {
49-
("subsec_micros", 1_000) => "subsec_millis",
49+
("subsec_micros", 1_000) | ("subsec_nanos", 1_000_000) => "subsec_millis",
5050
("subsec_nanos", 1_000) => "subsec_micros",
51-
("subsec_nanos", 1_000_000) => "subsec_millis",
5251
_ => return,
5352
};
5453
span_lint_and_sugg(

clippy_lints/src/inherent_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct Pass {
4646

4747
impl Default for Pass {
4848
fn default() -> Self {
49-
Pass { impls: FxHashMap::default() }
49+
Self { impls: FxHashMap::default() }
5050
}
5151
}
5252

clippy_lints/src/methods/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,22 +1452,33 @@ fn lint_get_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr, get_args: &[hir::
14521452
// Note: we don't want to lint `get_mut().unwrap` for HashMap or BTreeMap,
14531453
// because they do not implement `IndexMut`
14541454
let expr_ty = cx.tables.expr_ty(&get_args[0]);
1455+
let get_args_str = if get_args.len() > 1 {
1456+
snippet(cx, get_args[1].span, "_")
1457+
} else {
1458+
return; // not linting on a .get().unwrap() chain or variant
1459+
};
1460+
let needs_ref;
14551461
let caller_type = if derefs_to_slice(cx, &get_args[0], expr_ty).is_some() {
1462+
needs_ref = get_args_str.parse::<usize>().is_ok();
14561463
"slice"
14571464
} else if match_type(cx, expr_ty, &paths::VEC) {
1465+
needs_ref = get_args_str.parse::<usize>().is_ok();
14581466
"Vec"
14591467
} else if match_type(cx, expr_ty, &paths::VEC_DEQUE) {
1468+
needs_ref = get_args_str.parse::<usize>().is_ok();
14601469
"VecDeque"
14611470
} else if !is_mut && match_type(cx, expr_ty, &paths::HASHMAP) {
1471+
needs_ref = true;
14621472
"HashMap"
14631473
} else if !is_mut && match_type(cx, expr_ty, &paths::BTREEMAP) {
1474+
needs_ref = true;
14641475
"BTreeMap"
14651476
} else {
14661477
return; // caller is not a type that we want to lint
14671478
};
14681479

14691480
let mut_str = if is_mut { "_mut" } else { "" };
1470-
let borrow_str = if is_mut { "&mut " } else { "&" };
1481+
let borrow_str = if !needs_ref { "" } else if is_mut { "&mut " } else { "&" };
14711482
span_lint_and_sugg(
14721483
cx,
14731484
GET_UNWRAP,
@@ -1482,7 +1493,7 @@ fn lint_get_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr, get_args: &[hir::
14821493
"{}{}[{}]",
14831494
borrow_str,
14841495
snippet(cx, get_args[0].span, "_"),
1485-
snippet(cx, get_args[1].span, "_")
1496+
get_args_str
14861497
),
14871498
);
14881499
}

clippy_lints/src/multiple_crate_versions.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,17 @@ impl LintPass for Pass {
4141

4242
impl EarlyLintPass for Pass {
4343
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
44-
let metadata = match cargo_metadata::metadata_deps(None, true) {
45-
Ok(metadata) => metadata,
46-
Err(_) => {
47-
span_lint(
48-
cx,
49-
MULTIPLE_CRATE_VERSIONS,
50-
krate.span,
51-
"could not read cargo metadata"
52-
);
44+
let metadata = if let Ok(metadata) = cargo_metadata::metadata_deps(None, true) {
45+
metadata
46+
} else {
47+
span_lint(
48+
cx,
49+
MULTIPLE_CRATE_VERSIONS,
50+
krate.span,
51+
"could not read cargo metadata"
52+
);
5353

54-
return;
55-
}
54+
return;
5655
};
5756

5857
let mut packages = metadata.packages;

clippy_lints/src/utils/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ define_Conf! {
148148
}
149149

150150
impl Default for Conf {
151-
fn default() -> Conf {
151+
fn default() -> Self {
152152
toml::from_str("").expect("we never error on empty config files")
153153
}
154154
}

tests/ui/get_unwrap.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ fn main() {
4343
*some_btreemap.get_mut(&1).unwrap() = 'b';
4444
*false_positive.get_mut(0).unwrap() = 1;
4545
}
46+
47+
{ // Test `get().unwrap().foo()` and `get_mut().unwrap().bar()`
48+
let _ = some_vec.get(0..1).unwrap().to_vec();
49+
let _ = some_vec.get_mut(0..1).unwrap().to_vec();
50+
}
4651
}

0 commit comments

Comments
 (0)