Skip to content

Recommend fix count() -> len() on slices #87614

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

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ symbols! {
core_panic_macro,
cosf32,
cosf64,
count,
cr,
crate_id,
crate_in_paths,
Expand Down
26 changes: 23 additions & 3 deletions compiler/rustc_typeck/src/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

fn is_slice_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
self.autoderef(span, ty).any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
}

pub fn report_method_error(
&self,
mut span: Span,
Expand Down Expand Up @@ -691,7 +695,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let mut restrict_type_params = false;
let mut unsatisfied_bounds = false;
if !unsatisfied_predicates.is_empty() {
if item_name.name == sym::count && self.is_slice_ty(actual, span) {
let msg = "consider using `len` instead";
if let SelfSource::MethodCall(_expr) = source {
err.span_suggestion_short(
span,
msg,
String::from("len"),
Applicability::MachineApplicable,
);
} else {
err.span_label(span, msg);
}
if let Some(iterator_trait) = self.tcx.get_diagnostic_item(sym::Iterator) {
let iterator_trait = self.tcx.def_path_str(iterator_trait);
err.note(&format!("`count` is defined on `{iterator_trait}`, which `{actual}` does not implement"));
}
} else if !unsatisfied_predicates.is_empty() {
let def_span = |def_id| {
self.tcx.sess.source_map().guess_head_span(self.tcx.def_span(def_id))
};
Expand Down Expand Up @@ -990,9 +1010,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

let mut fallback_span = true;
let msg = "remove this method call";
if item_name.name == sym::as_str && actual.peel_refs().is_str() {
let msg = "remove this method call";
let mut fallback_span = true;
if let SelfSource::MethodCall(expr) = source {
let call_expr =
self.tcx.hir().expect_expr(self.tcx.hir().get_parent_node(expr.hir_id));
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/suggestions/count2len.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let slice = [1,2,3,4];
let vec = vec![1,2,3,4];

slice.count(); //~ERROR: E0599
vec.count(); //~ERROR: E0599
vec.as_slice().count(); //~ERROR: E0599
}
36 changes: 36 additions & 0 deletions src/test/ui/suggestions/count2len.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error[E0599]: no method named `count` found for array `[{integer}; 4]` in the current scope
--> $DIR/count2len.rs:5:11
|
LL | slice.count();
| ^^^^^
| |
| method cannot be called on `[{integer}; 4]` due to unsatisfied trait bounds
| help: consider using `len` instead
|
= note: `count` is defined on `Iterator`, which `[{integer}; 4]` does not implement

error[E0599]: no method named `count` found for struct `Vec<{integer}>` in the current scope
--> $DIR/count2len.rs:6:9
|
LL | vec.count();
| ^^^^^
| |
| method cannot be called on `Vec<{integer}>` due to unsatisfied trait bounds
| help: consider using `len` instead
|
= note: `count` is defined on `Iterator`, which `Vec<{integer}>` does not implement

error[E0599]: no method named `count` found for reference `&[{integer}]` in the current scope
--> $DIR/count2len.rs:7:20
|
LL | vec.as_slice().count();
| ^^^^^
| |
| method cannot be called on `&[{integer}]` due to unsatisfied trait bounds
| help: consider using `len` instead
|
= note: `count` is defined on `Iterator`, which `&[{integer}]` does not implement

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0599`.