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

the default value of Edition field is 2015, it can't tell the difference edition set with 2015 and edition not set. #250

Open
linyihai opened this issue Sep 14, 2023 · 3 comments

Comments

@linyihai
Copy link

How to know that rust edition not set in Cargo.toml? I want to know whethe edition is set or not in the Cargo.toml.
In this situation, I found that the Edition offered by the cargo_metadata crate set the default value 2015, so I had no idea to know rust edition is not set in cargo.toml, because it will be set the default value 2015 when serde_json crate parse the toml file.

the relative code is below:

https://github.com/oli-obk/cargo_metadata/blob/main/src/lib.rs

    /// Beware that individual targets may specify their own edition in
    /// [`Target::edition`].
    #[serde(default)]
    pub edition: Edition,

    fn default() -> Self {
        Self::E2015
    }
}

Forgive my poor english expression since i am not native english speaker. My need is to know whethe edition is set or not in the Cargo.toml. Maybe the cargo_metadata can offer a way to tell the edition not set in the Cargo.toml, like this

    fn had_set() -> bool {
        TODO!()
    }
@weihanglo
Copy link
Contributor

cargo_metadata is a consumer of the output of cargo metadata command. cargo metadata sets "edition": "2015" when the field is missing in Cargo.toml.

I don't think this crate cargo_metadata can detect the situation you describe.

My need is to know whethe edition is set or not in the Cargo.toml

If possible, please share why you need to know it is not set, and maybe we can come up with a better alternative.

@linyihai
Copy link
Author

@weihanglo thanks for you reply. The background is that i make a custom lint to check whethe the rust edition is set or not. the detail is:

cargo_metadata is also used by rust-clippy repository. in my pr as above, i get the rust edition offered by cargo_metadata.

use super::NO_RUST_EDITION;
use cargo_metadata::Metadata;
use clippy_utils::diagnostics::span_lint;
use rustc_lint::LateContext;
use rustc_span::source_map::DUMMY_SP;

pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata) {
    for package in &metadata.packages {
        // indeed, rust editon allow omited and the default value is 2015. to avoid read all cargol.toml
        // file and make it simple, if the rust edition newer than the default(2015), then it can be
        // treated had rust edition configed
        if package.edition < cargo_metadata::Edition::E2018 {
            let message = format!("package `{}` is missing `edition` metadata", package.name);
            span_lint(cx, NO_RUST_EDITION, DUMMY_SP, &message);
        }
    }
}

without the cargo_metadata, i have no way to get the rust edition unless i write my cargo.toml parser function.

@weihanglo
Copy link
Contributor

My personal stance would be like: the lint should either be implemented in Cargo or reuse the TOML manifest parser in Cargo. Either way, I agree cargo_metadata at this point has no way to provide what you're looking for.

See this zulip discussion for more: https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/Cargo.20Lints/near/389439777

@linyihai linyihai reopened this Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants