Skip to content

Why is replace_consts enabled by default? #2380

Closed

Description

@gnzlbg

The replace_consts lint is enabled in clippy-pedantic by default.

The docs hints that using consts is bad because const fn exists.

This does not make any sense to me: why should const fn be preferred over consts? Are associated consts also bad?

Activity

  1. oli-obk commented on Jan 18, 2018

    @oli-obk
    Contributor

    Are associated consts also bad?

    no, but there are none ;)

    std::i32::MAX is not i32::MAX, but i32::max_value() is on the type

    This does not make any sense to me: why should const fn be preferred over consts?

    the const fns for integers are disputed, but the const fns for atomics are the way to go, the old constants were hacks that existed because of the lack of const fn.

    see also the discussion on the PR adding this lint #2344

  2. gnzlbg commented on Jan 18, 2018

    @gnzlbg
    ContributorAuthor

    FWIW I got this error on std::usize::MAX.

    but the const fns for atomics are the way to go

    This makes sense.

  3. gnzlbg commented on Jan 18, 2018

    @gnzlbg
    ContributorAuthor

    Maybe what this lint needs is just better docs? The argument that ::std::usize::MAX needs the whole path, but usize::max_value() does not because it works on the type is a good one actually.

  4. added
    good first issueThese issues are a good way to get started with Clippy
    A-documentationArea: Adding or improving documentation
    on Jan 18, 2018
  5. gnzlbg commented on Jan 19, 2018

    @gnzlbg
    ContributorAuthor

    So I think the main arguments are:

    • it is more idiomatic to directly associate constants with a type than to put them in a module that only contains the type using associcated consts or const fn
    • this allows you to use the constant without the full path, like so usize::max_value().
    • now that associated consts are stable there is discussion about moving these constants into these types, and deprecating the module level ones (so using the module level ones should not be encouraged)

    @petrochenkov you had strong thoughts about this. How would you document this?

  6. added 2 commits that reference this issue on Jun 12, 2019
  7. added a commit that references this issue on Jul 18, 2019
  8. phansch commented on Mar 5, 2020

    @phansch
    Contributor

    rust-lang/rust#68952 stabilized the assoc_int_consts feature for Rust 1.34. For Clippy this is relevant for at least the replace_consts lint:

    As far as I can tell, that lint was meant to suggest to use the associated methods on the integer primitives, instead of the constants that have now been soft-deprecated in rust-lang/rust#68952. Although, the lint doesn't really provide a reason why one is better over the other. (Please correct me if I misunderstood that lint)

    The RFC also mentions a deprecation of the associated functions the lint is currently suggesting, so we might want to deprecate replace_consts altogether.

  9. oli-obk commented on Mar 5, 2020

    @oli-obk
    Contributor

    We can keep it but suggest moving from module consts to associated constant instead of to associated functions

  10. tesuji commented on Apr 1, 2020

    @tesuji
    Contributor

    This could be closed now with #5380.

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-documentationArea: Adding or improving documentationgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions