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

Fix type suggestion for manual_is_ascii_check #13916

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 7 additions & 11 deletions clippy_lints/src/manual_is_ascii_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualIsAsciiCheck {
&& !matches!(cx.typeck_results().expr_ty(arg).peel_refs().kind(), ty::Param(_))
{
let arg = peel_ref_operators(cx, arg);
let ty_sugg = get_ty_sugg(cx, arg, start);
let ty_sugg = get_ty_sugg(cx, arg);
let range = check_range(start, end);
check_is_ascii(cx, expr.span, arg, &range, ty_sugg);
}
Expand All @@ -123,18 +123,14 @@ impl<'tcx> LateLintPass<'tcx> for ManualIsAsciiCheck {
extract_msrv_attr!(LateContext);
}

fn get_ty_sugg(cx: &LateContext<'_>, arg: &Expr<'_>, bound_expr: &Expr<'_>) -> Option<(Span, &'static str)> {
if let ExprKind::Lit(lit) = bound_expr.kind
&& let local_hid = path_to_local(arg)?
&& let Node::Param(Param { ty_span, span, .. }) = cx.tcx.parent_hir_node(local_hid)
fn get_ty_sugg(cx: &LateContext<'_>, arg: &Expr<'_>) -> Option<(Span, String)> {
let local_hid = path_to_local(arg)?;
if let Node::Param(Param { ty_span, span, .. }) = cx.tcx.parent_hir_node(local_hid)
// `ty_span` and `span` are the same for inferred type, thus a type suggestion must be given
&& ty_span == span
{
let ty_str = match lit.node {
Char(_) => "char",
Byte(_) => "u8",
_ => return None,
};
let arg_type = cx.typeck_results().expr_ty(arg);
let ty_str = arg_type.to_string();
return Some((*ty_span, ty_str));
Comment on lines +133 to 134
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Just one nit, we can return the Ty here to avoid allocating the extra string since it implements Display

}
None
Expand All @@ -145,7 +141,7 @@ fn check_is_ascii(
span: Span,
recv: &Expr<'_>,
range: &CharRange,
ty_sugg: Option<(Span, &'_ str)>,
ty_sugg: Option<(Span, String)>,
) {
let sugg = match range {
CharRange::UpperChar => "is_ascii_uppercase",
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/manual_is_ascii_check.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ fn generics() {
take_while(|c: u8| c.is_ascii_uppercase());
take_while(|c: char| c.is_ascii_uppercase());
}

fn adds_type_reference() {
let digits: Vec<&char> = ['1', 'A'].iter().take_while(|c: &&char| c.is_ascii_digit()).collect();
let digits: Vec<&mut char> = ['1', 'A'].iter_mut().take_while(|c: &&mut char| c.is_ascii_digit()).collect();
}
5 changes: 5 additions & 0 deletions tests/ui/manual_is_ascii_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ fn generics() {
take_while(|c| (b'A'..=b'Z').contains(&c));
take_while(|c: char| ('A'..='Z').contains(&c));
}

fn adds_type_reference() {
let digits: Vec<&char> = ['1', 'A'].iter().take_while(|c| ('0'..='9').contains(c)).collect();
let digits: Vec<&mut char> = ['1', 'A'].iter_mut().take_while(|c| ('0'..='9').contains(c)).collect();
}
24 changes: 23 additions & 1 deletion tests/ui/manual_is_ascii_check.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,27 @@ error: manual check for common ascii range
LL | take_while(|c: char| ('A'..='Z').contains(&c));
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `c.is_ascii_uppercase()`

error: aborting due to 27 previous errors
error: manual check for common ascii range
--> tests/ui/manual_is_ascii_check.rs:87:63
|
LL | let digits: Vec<&char> = ['1', 'A'].iter().take_while(|c| ('0'..='9').contains(c)).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | let digits: Vec<&char> = ['1', 'A'].iter().take_while(|c: &&char| c.is_ascii_digit()).collect();
| ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~

error: manual check for common ascii range
--> tests/ui/manual_is_ascii_check.rs:88:71
|
LL | let digits: Vec<&mut char> = ['1', 'A'].iter_mut().take_while(|c| ('0'..='9').contains(c)).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | let digits: Vec<&mut char> = ['1', 'A'].iter_mut().take_while(|c: &&mut char| c.is_ascii_digit()).collect();
| ~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~

error: aborting due to 29 previous errors

Loading