Skip to content

Commit

Permalink
Reject inclusive range without end expression
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Sep 6, 2023
1 parent a39aaf0 commit 5c4ccc0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/version.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fmt, str::FromStr};

use anyhow::{Context as _, Error, Result};
use anyhow::{bail, Context as _, Error, Result};

#[derive(Copy, Clone)]
pub(crate) struct Version {
Expand Down Expand Up @@ -57,9 +57,9 @@ impl fmt::Display for VersionRange {
if let MaybeVersion::Version(start) = self.start_inclusive {
write!(f, "{start}")?;
}
write!(f, "..=")?;
write!(f, "..")?;
if let MaybeVersion::Version(end) = self.end_inclusive {
write!(f, "{end}")?;
write!(f, "={end}")?;
}
Ok(())
}
Expand All @@ -71,12 +71,24 @@ impl FromStr for VersionRange {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (start, end) = if let Some((start, end)) = s.split_once("..") {
let end = match end.strip_prefix('=') {
Some(end) => end,
Some(end) => {
if end.is_empty() {
// Reject inclusive range without end expression (`..=` and `<start>..=`). (same behavior as Rust's inclusive range)
bail!(
"inclusive range `{s}` must have end expression; consider using `{}` or `{s}<end>`",
s.replace("..=", "..")
)
}
end
}
None => {
warn!(
"using `..` for inclusive range is deprecated; consider using `{}`",
s.replace("..", "..=")
);
// `..` and `<start>..` are okay, so only warn `<start>..<end>`.
if !end.is_empty() {
warn!(
"using `..` for inclusive range is deprecated; consider using `{}`",
s.replace("..", "..=")
);
}
end
}
};
Expand Down
8 changes: 8 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,14 @@ fn version_range_failure() {
.assert_failure("real")
.stderr_contains("major version must be 1");

// inclusive range without end expression
cargo_hack(["check", "--version-range", "..="]).assert_failure("real").stderr_contains(
"inclusive range `..=` must have end expression; consider using `..` or `..=<end>`",
);
cargo_hack(["check", "--version-range", "1.45..="]).assert_failure("real").stderr_contains(
"inclusive range `1.45..=` must have end expression; consider using `1.45..` or `1.45..=<end>`",
);

// patch version
cargo_hack(["check", "--version-range", "1.45.2.."])
.assert_failure("real") // warn
Expand Down

0 comments on commit 5c4ccc0

Please sign in to comment.