Skip to content

Commit

Permalink
Auto merge of #13469 - nyurik:apply-ref-option, r=llogiq
Browse files Browse the repository at this point in the history
Convert `&Option<T>` to `Option<&T>`

Run `ref_option` (#13336) on the Clippy's own code, quiet a few hits. Per mentioned video, this may actually improve performance as well.  Switch lint to `pedantic`

----

changelog: [`ref_option`]: upgrade lint to `pedantic`
  • Loading branch information
bors committed Sep 29, 2024
2 parents 7b566c2 + f7d5d9d commit 061004a
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 83 deletions.
16 changes: 8 additions & 8 deletions clippy_lints/src/cargo/common_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata, ignore_publish: b
// only run the lint if publish is `None` (`publish = true` or skipped entirely)
// or if the vector isn't empty (`publish = ["something"]`)
if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || ignore_publish {
if is_empty_str(&package.description) {
if is_empty_str(package.description.as_ref()) {
missing_warning(cx, package, "package.description");
}

if is_empty_str(&package.license) && is_empty_str(&package.license_file) {
if is_empty_str(package.license.as_ref()) && is_empty_str(package.license_file.as_ref()) {
missing_warning(cx, package, "either package.license or package.license_file");
}

if is_empty_str(&package.repository) {
if is_empty_str(package.repository.as_ref()) {
missing_warning(cx, package, "package.repository");
}

if is_empty_str(&package.readme) {
if is_empty_str(package.readme.as_ref()) {
missing_warning(cx, package, "package.readme");
}

if is_empty_vec(&package.keywords) {
if is_empty_vec(package.keywords.as_ref()) {
missing_warning(cx, package, "package.keywords");
}

if is_empty_vec(&package.categories) {
if is_empty_vec(package.categories.as_ref()) {
missing_warning(cx, package, "package.categories");
}
}
Expand All @@ -42,8 +42,8 @@ fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, fiel
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, message);
}

fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: &Option<T>) -> bool {
value.as_ref().map_or(true, |s| s.as_ref().is_empty())
fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: Option<&T>) -> bool {
value.map_or(true, |s| s.as_ref().is_empty())
}

fn is_empty_vec(value: &[String]) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ declare_clippy_lint! {
/// ```
#[clippy::version = "1.82.0"]
pub REF_OPTION,
nursery,
pedantic,
"function signature uses `&Option<T>` instead of `Option<&T>`"
}

Expand Down
11 changes: 7 additions & 4 deletions clippy_lints/src/unnested_or_patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,15 @@ fn transform_with_focus_on_idx(alternatives: &mut ThinVec<P<Pat>>, focus_idx: us
|k, ps1, idx| matches!(
k,
TupleStruct(qself2, path2, ps2)
if eq_maybe_qself(qself1, qself2) && eq_path(path1, path2) && eq_pre_post(ps1, ps2, idx)
if eq_maybe_qself(qself1.as_ref(), qself2.as_ref())
&& eq_path(path1, path2) && eq_pre_post(ps1, ps2, idx)
),
|k| always_pat!(k, TupleStruct(_, _, ps) => ps),
),
// Transform a record pattern `S { fp_0, ..., fp_n }`.
Struct(qself1, path1, fps1, rest1) => extend_with_struct_pat(qself1, path1, fps1, *rest1, start, alternatives),
Struct(qself1, path1, fps1, rest1) => {
extend_with_struct_pat(qself1.as_ref(), path1, fps1, *rest1, start, alternatives)
},
};

alternatives[focus_idx].kind = focus_kind;
Expand All @@ -292,7 +295,7 @@ fn transform_with_focus_on_idx(alternatives: &mut ThinVec<P<Pat>>, focus_idx: us
/// So when we fixate on some `ident_k: pat_k`, we try to find `ident_k` in the other pattern
/// and check that all `fp_i` where `i ∈ ((0...n) \ k)` between two patterns are equal.
fn extend_with_struct_pat(
qself1: &Option<P<ast::QSelf>>,
qself1: Option<&P<ast::QSelf>>,
path1: &ast::Path,
fps1: &mut [ast::PatField],
rest1: ast::PatFieldsRest,
Expand All @@ -307,7 +310,7 @@ fn extend_with_struct_pat(
|k| {
matches!(k, Struct(qself2, path2, fps2, rest2)
if rest1 == *rest2 // If one struct pattern has `..` so must the other.
&& eq_maybe_qself(qself1, qself2)
&& eq_maybe_qself(qself1, qself2.as_ref())
&& eq_path(path1, path2)
&& fps1.len() == fps2.len()
&& fps1.iter().enumerate().all(|(idx_1, fp1)| {
Expand Down
Loading

0 comments on commit 061004a

Please sign in to comment.