Skip to content

"-NUM.method()" is parsed in surprising ways #117155

Closed

Description

This came up here but I am pretty sure I ran into this before: Rust parses -5.5f32.method() as -(5.5f32.method()). I don't know if that is the parsing result that anyone expects, I certainly would never expect it -5.5f32 is a float literals, and that is what I want to apply the method to, I never even considered that the compiler might interpret this as a unary minus applied to the method result.

Assuming that we cannot change the parser, I think we should spend some effort defending against the possible confusion here. For instance:

  • lint against -5.5f32.method() (suggesting to write -(5.5f32.method()) if that's really what one meant)
  • have rustfmt add the parentheses that make precedence explicit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-grammarArea: The grammar of RustA-lintArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions