Skip to content

rustc::lint-API: Distinguish directly applicable from colloquial code suggestions #33691

Closed

Description

@llogiq

There has been some inquiry into a "quick fix" IDE feature for Rust. Given that lints (including clippy) already generate suggestions based on code, a quick win could be made by allowing the lints to distinguish directly applicable suggestions and perhaps somehow flag those for tools using rustc's JSON output. Then IDEs could weave the suggestions into the code based on the reported span.

cc @jonathandturner @nrc @Manishearth @mcarton @birkenfeld

Activity

  1. Manishearth commented on May 17, 2016

    @Manishearth
    Member

    IMO such a feature would probably be more involved than just span_suggestion, with hooks for replacing individual bits of code (instead of text substitution) in a typed fashion. But if not, a distinguishing span_help_code would be nice.

  2. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    on May 18, 2016
  3. nrc commented on May 18, 2016

    @nrc
    Member

    span_suggestion probably needs an overhaul, but would be a good starting point. The suggestion JSON should be able to be nested in JSON errors or emitted individually (e.g., Clippy would do the latter, although perhaps it could just emit JSON errors when requested with the embedded suggestion).

    @Manishearth what does "in a typed fashion" mean? I would expect that all the hard processing would be done by the tool generating the suggestion (rustc, clippy) and the suggestion is just a set of spans and replacement text (where the replacement text might be the empty string for deletions and the span to be replaced might be zero-length for insertions).

    We might also want to let the replacing tool format the text, so the suggestion text could be some kind of reification of the AST. That has horrible problems. Alternatively the suggestion could be unformatted source code and if the replacement tool wants to reformat, it can re-parse and reformat.

  4. Manishearth commented on May 18, 2016

    @Manishearth
    Member

    what does "in a typed fashion" mean?

    Currently clippy takes span_to_snippets and splices them together to get suggestions. It would be nicer if we could take the typed AST objects and splice them together (e.g. moving an expression from an if block to the outside by appending it to the outer block) and print them, while retaining formatting and macros. There's no way to do that currently, and we have to do random hacks to get indentation to work.

    But yeah, splice-and-feed-to-rustfmt sort of works too.

  5. oli-obk commented on May 18, 2016

    @oli-obk
    Contributor

    Related internals post: https://internals.rust-lang.org/t/rustcs-json-output-format/3446/11?u=ker

    if things like unresolved name xyz, did you mean xyz1 are changed to use suggestions, we'd need a way to distinguish between guaranteed correct suggestions like changing !(a == b) into a != b against heuristic suggestions like changing xyz into xyz1 or like adding new use x::X; items when X is not found in the scope.

  6. Manishearth commented on May 18, 2016

    @Manishearth
    Member

    Yeah, I think in that case we don't want to output code in the first place

  7. oli-obk commented on May 18, 2016

    @oli-obk
    Contributor

    Yeah, I think in that case we don't want to output code in the first place

    Not sure I understand? If there's an ambiguity, the user can decide, but often it's still possible to provide sensible choices, so the user can click on a choice and the IDE inserts that one.

    Are you suggesting to never provide snippets if there are ambiguities or heuristics involved?

  8. Manishearth commented on May 18, 2016

    @Manishearth
    Member

    I guess? We'd need a clear distinction then, right.

  9. sanxiyn commented on May 18, 2016

    @sanxiyn
    Contributor

    I think it is enough to simply clarify that span_suggestion is only for directly applicable suggestions. In fact, have we ever used span_suggestion when it is not directly applicable?

  10. Manishearth commented on May 18, 2016

    @Manishearth
    Member

    Yeah, that was my point -- we don't do it now and we can continue not doing it.

  11. llogiq commented on May 18, 2016

    @llogiq
    ContributorAuthor

    I think we have two interesting distinctions to make:

    1. If the suggestion can be directly applied to the code (that's what this issue is about)
    2. How many alternative suggestions we have and how to sort them (my vote is sort by likelyhood, though we may need to find good enough heuristics)
  12. added
    T-dev-toolsRelevant to the dev-tools subteam, which will review and decide on the PR/issue.
    and removed on May 22, 2017
  13. oli-obk commented on Aug 17, 2018

    @oli-obk
    Contributor

    I think this has been resolved by the Applicability annotations

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-dev-toolsRelevant to the dev-tools subteam, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions