Skip to content

Commit e8f79f3

Browse files
committed
feat: Add lint to detect missing rust-version field
1 parent 4b05f50 commit e8f79f3

File tree

10 files changed

+83
-1
lines changed

10 files changed

+83
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5828,6 +5828,7 @@ Released 2018-09-13
58285828
[`missing_fields_in_debug`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_fields_in_debug
58295829
[`missing_inline_in_public_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_inline_in_public_items
58305830
[`missing_panics_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc
5831+
[`missing_rust_version`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_rust_version
58315832
[`missing_safety_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_safety_doc
58325833
[`missing_spin_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_spin_loop
58335834
[`missing_trait_methods`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_trait_methods
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use cargo_metadata::Metadata;
2+
use clippy_utils::diagnostics::span_lint;
3+
use clippy_utils::msrvs::{self, Msrv};
4+
use rustc_lint::LateContext;
5+
use rustc_span::DUMMY_SP;
6+
7+
use super::MISSING_RUST_VERSION;
8+
9+
pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata, msrv: &Msrv) {
10+
// see <https://github.com/oli-obk/cargo_metadata/issues/253>
11+
if !msrv.meets(msrvs::RUST_VERSION_FIELD) {
12+
return;
13+
}
14+
15+
for package in &metadata.packages {
16+
if package.rust_version.is_none() {
17+
let message = format!("package `{}` is missing the `rust-version` field", package.name);
18+
span_lint(cx, MISSING_RUST_VERSION, DUMMY_SP, message);
19+
}
20+
}
21+
}

clippy_lints/src/cargo/mod.rs

+34
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
mod common_metadata;
22
mod feature_name;
33
mod lint_groups_priority;
4+
mod missing_rust_version;
45
mod multiple_crate_versions;
56
mod wildcard_dependencies;
67

78
use cargo_metadata::MetadataCommand;
89
use clippy_config::Conf;
910
use clippy_utils::diagnostics::span_lint;
1011
use clippy_utils::is_lint_allowed;
12+
use clippy_utils::msrvs::Msrv;
1113
use rustc_data_structures::fx::FxHashSet;
1214
use rustc_hir::hir_id::CRATE_HIR_ID;
1315
use rustc_lint::{LateContext, LateLintPass, Lint};
@@ -213,9 +215,37 @@ declare_clippy_lint! {
213215
"a lint group in `Cargo.toml` at the same priority as a lint"
214216
}
215217

218+
declare_clippy_lint! {
219+
/// ### What it does
220+
/// Checks to see if the `rust-version` field is defined in `Cargo.toml`.
221+
///
222+
/// ### Why is this bad?
223+
/// Starting with version 3, the [resolver] takes this field into account
224+
/// when picking dependencies.
225+
///
226+
/// ### Example
227+
/// ```toml
228+
/// [package]
229+
/// # ...
230+
/// ```
231+
/// Use instead:
232+
/// ```toml
233+
/// [package]
234+
/// # ...
235+
/// rust-version = "1.84"
236+
/// ```
237+
///
238+
/// [resolver]: https://doc.rust-lang.org/cargo/reference/resolver.html#rust-version
239+
#[clippy::version = "1.86.0"]
240+
pub MISSING_RUST_VERSION,
241+
cargo,
242+
"the `rust-version` field is not defined in `Cargo.toml`"
243+
}
244+
216245
pub struct Cargo {
217246
allowed_duplicate_crates: FxHashSet<String>,
218247
ignore_publish: bool,
248+
msrv: Msrv,
219249
}
220250

221251
impl_lint_pass!(Cargo => [
@@ -225,13 +255,15 @@ impl_lint_pass!(Cargo => [
225255
MULTIPLE_CRATE_VERSIONS,
226256
WILDCARD_DEPENDENCIES,
227257
LINT_GROUPS_PRIORITY,
258+
MISSING_RUST_VERSION,
228259
]);
229260

230261
impl Cargo {
231262
pub fn new(conf: &'static Conf) -> Self {
232263
Self {
233264
allowed_duplicate_crates: conf.allowed_duplicate_crates.iter().cloned().collect(),
234265
ignore_publish: conf.cargo_ignore_publish,
266+
msrv: conf.msrv.clone(),
235267
}
236268
}
237269
}
@@ -243,6 +275,7 @@ impl LateLintPass<'_> for Cargo {
243275
REDUNDANT_FEATURE_NAMES,
244276
NEGATIVE_FEATURE_NAMES,
245277
WILDCARD_DEPENDENCIES,
278+
MISSING_RUST_VERSION,
246279
];
247280
static WITH_DEPS_LINTS: &[&Lint] = &[MULTIPLE_CRATE_VERSIONS];
248281

@@ -256,6 +289,7 @@ impl LateLintPass<'_> for Cargo {
256289
Ok(metadata) => {
257290
common_metadata::check(cx, &metadata, self.ignore_publish);
258291
feature_name::check(cx, &metadata);
292+
missing_rust_version::check(cx, &metadata, &self.msrv);
259293
wildcard_dependencies::check(cx, &metadata);
260294
},
261295
Err(e) => {

clippy_lints/src/declared_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
7272
crate::byte_char_slices::BYTE_CHAR_SLICES_INFO,
7373
crate::cargo::CARGO_COMMON_METADATA_INFO,
7474
crate::cargo::LINT_GROUPS_PRIORITY_INFO,
75+
crate::cargo::MISSING_RUST_VERSION_INFO,
7576
crate::cargo::MULTIPLE_CRATE_VERSIONS_INFO,
7677
crate::cargo::NEGATIVE_FEATURE_NAMES_INFO,
7778
crate::cargo::REDUNDANT_FEATURE_NAMES_INFO,

clippy_utils/src/msrvs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ msrv_aliases! {
3333
1,63,0 { CLONE_INTO }
3434
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE, CONST_EXTERN_C_FN }
3535
1,59,0 { THREAD_LOCAL_CONST_INIT }
36-
1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY, CONST_RAW_PTR_DEREF }
36+
1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY, CONST_RAW_PTR_DEREF, RUST_VERSION_FIELD }
3737
1,56,0 { CONST_FN_UNION }
3838
1,55,0 { SEEK_REWIND }
3939
1,54,0 { INTO_KEYS }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: package `missing_rust_version` is missing the `rust-version` field
2+
|
3+
= note: `-D clippy::missing-rust-version` implied by `-D warnings`
4+
= help: to override `-D warnings` add `#[allow(clippy::missing_rust_version)]`
5+
6+
error: could not compile `missing_rust_version` (bin "missing_rust_version") due to 1 previous error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "missing_rust_version"
3+
version = "0.1.0"
4+
publish = false
5+
6+
[workspace]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![warn(clippy::missing_rust_version)]
2+
3+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "missing_rust_version"
3+
version = "0.1.0"
4+
rust-version = "1.84"
5+
publish = false
6+
7+
[workspace]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![warn(clippy::missing_rust_version)]
2+
3+
fn main() {}

0 commit comments

Comments
 (0)