Skip to content

[WIP] suggest parsing string into numeric type on invalid cast - #160578

Draft
Snxhit wants to merge 1 commit into
rust-lang:mainfrom
Snxhit:issue-159494-parse-suggestion
Draft

[WIP] suggest parsing string into numeric type on invalid cast#160578
Snxhit wants to merge 1 commit into
rust-lang:mainfrom
Snxhit:issue-159494-parse-suggestion

Conversation

@Snxhit

@Snxhit Snxhit commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

fixes #159494

changes:
suggest parsing on invalid string-to-number casts, add ui test for the same.

wip:
suggest parsing on assignment target type mismatch, value checking before suggestion, final ui test.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 5, 2026
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#14 2.884 Building wheels for collected packages: reuse
#14 2.885   Building wheel for reuse (pyproject.toml): started
#14 3.100   Building wheel for reuse (pyproject.toml): finished with status 'done'
#14 3.102   Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132857 sha256=6a78a69541e3df9c7c106367722bab90241125ee6b7cdb81c5e40637268f85a9
#14 3.102   Stored in directory: /tmp/pip-ephem-wheel-cache-5j_2jg7p/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#14 3.104 Successfully built reuse
#14 3.105 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#14 3.507 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#14 3.507 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#14 4.043 Collecting virtualenv
---
                         err.multipart_suggestion(
                             "consider parsing the string into a numeric type instead",
-                            vec![
-                                (
-                                    self.expr_span.shrink_to_hi().to(self.cast_span),
-                                    format!(".parse::<{target_ty_str}>().unwrap_or_default()"),
-                                ),
-                            ],
+                            vec![(
+                                self.expr_span.shrink_to_hi().to(self.cast_span),
+                                format!(".parse::<{target_ty_str}>().unwrap_or_default()"),
+                            )],
                             rustc_errors::Applicability::MaybeIncorrect,
                         );
                     }
Diff in /checkout/compiler/rustc_hir_typeck/src/cast.rs:608:
                     if let Some(note) = note {
                         err.note(note);
                     }
-                    
+
                     if self.cast_ty.is_numeric() {
                         let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
 
Diff in /checkout/compiler/rustc_hir_typeck/src/cast.rs:621:
 
                             err.multipart_suggestion(
                                 "consider parsing the string into a numeric type instead",
-                                vec![
-                                    (
-                                        self.expr_span.shrink_to_hi().to(self.cast_span),
-                                        format!(".parse::<{target_ty_str}>().unwrap_or_default()")
-                                    ),
-                                ],
+                                vec![(
+                                    self.expr_span.shrink_to_hi().to(self.cast_span),
+                                    format!(".parse::<{target_ty_str}>().unwrap_or_default()"),
+                                )],
                                 rustc_errors::Applicability::MaybeIncorrect,
                             );
                         }
fmt: checked 7077 files
Bootstrap failed while executing `test src/tools/tidy tidyselftest --extra-checks=py,cpp,js,spellcheck`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest parse on expected types that implement FromStr

3 participants