Skip to content

Commit

Permalink
common: clippy lints
Browse files Browse the repository at this point in the history
Differential Revision: D59065418
  • Loading branch information
brianc118 authored and facebook-github-bot committed Jun 26, 2024
1 parent 5acde49 commit b2cbe18
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 26 deletions.
12 changes: 5 additions & 7 deletions below/common/src/cliutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const MISSING_SAMPLE_WARN_DURATION_S: u64 = 60;
pub fn system_time_from_date(date: &str) -> Result<SystemTime> {
Ok(UNIX_EPOCH
+ Duration::from_secs(
dateutil::HgTime::parse(&date)
dateutil::HgTime::parse(date)
.ok_or_else(|| {
anyhow!(
"Unrecognized timestamp format\n\
Expand Down Expand Up @@ -152,9 +152,8 @@ mod tests {

#[test]
fn test_system_time_from_date_fail() {
match system_time_from_date("invalid") {
Ok(_) => panic!("Expected to fail but didn't"),
Err(_) => {}
if system_time_from_date("invalid").is_ok() {
panic!("Expected to fail but didn't")
}
}

Expand All @@ -180,9 +179,8 @@ mod tests {

#[test]
fn test_system_time_from_date_and_adjuster_fail() {
match system_time_from_date_and_adjuster("2006-02-01 13:00:30 UTC", Some("invalid")) {
Ok(_) => panic!("Expected fo fail as adjuster is invalid"),
Err(_) => {}
if system_time_from_date_and_adjuster("2006-02-01 13:00:30 UTC", Some("invalid")).is_ok() {
panic!("Expected fo fail as adjuster is invalid")
}
}

Expand Down
20 changes: 10 additions & 10 deletions below/common/src/dateutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ impl HgTime {
Regex::new(r"(?i)(pm|am)$"),
) {
(Ok(relative_re), Ok(ampm_re)) => {
relative_re.is_match(&date) && !ampm_re.is_match(&date)
relative_re.is_match(date) && !ampm_re.is_match(date)
}
_ => false,
} =>
{
let date = date.to_ascii_lowercase();
let plus = date.starts_with("+") && !date.ends_with("ago");
let plus = date.starts_with('+') && !date.ends_with("ago");
let from_now = date.ends_with("from now");
// Trim past/future markers down to just date
let mut duration_str = if plus { &date[1..] } else { &date };
Expand Down Expand Up @@ -182,7 +182,7 @@ impl HgTime {
}
}
date if match Regex::new(r"^\d{10}$") {
Ok(systime_re) => systime_re.is_match(&date),
Ok(systime_re) => systime_re.is_match(date),
_ => false,
} =>
{
Expand Down Expand Up @@ -243,16 +243,16 @@ impl HgTime {
Self::from(date.and_hms_opt(23, 59, 59).unwrap()).use_default_offset() + 1;
Some(start..end)
}
date if date.starts_with(">") => {
date if date.starts_with('>') => {
Self::parse(&date[1..]).map(|start| start..Self::max_value())
}
date if date.starts_with("since ") => {
Self::parse(&date[6..]).map(|start| start..Self::max_value())
}
date if date.starts_with("<") => {
date if date.starts_with('<') => {
Self::parse(&date[1..]).map(|end| Self::min_value()..end)
}
date if date.starts_with("-") => {
date if date.starts_with('-') => {
// This does not really make much sense. But is supported by hg
// (see 'hg help dates').
Self::parse_range(&format!("since {} days ago", &date[1..]))
Expand All @@ -264,7 +264,7 @@ impl HgTime {
let phrases: Vec<_> = date.split(" to ").collect();
if phrases.len() == 2 {
if let (Some(start), Some(end)) =
(Self::parse(&phrases[0]), Self::parse(&phrases[1]))
(Self::parse(phrases[0]), Self::parse(phrases[1]))
{
Some(start..end)
} else {
Expand Down Expand Up @@ -299,7 +299,7 @@ impl HgTime {
let date = date.trim();

// Hg internal format. "unixtime offset"
let parts: Vec<_> = date.split(" ").collect();
let parts: Vec<_> = date.split(' ').collect();
if parts.len() == 2 {
if let Ok(unixtime) = parts[0].parse() {
if let Ok(offset) = parts[1].parse() {
Expand Down Expand Up @@ -343,7 +343,7 @@ impl HgTime {
// For example, if the user only specified "month/day",
// then we should use the current "year", instead of
// year 0.
let now = now.get_or_insert_with(|| Local::now());
let now = now.get_or_insert_with(Local::now);
date_with_defaults +=
&format!(" @{}", now.format(&format!("%{}", format_char)));
} else {
Expand Down Expand Up @@ -501,7 +501,7 @@ pub fn set_default_offset(offset: i32) {

fn is_valid_offset(offset: i32) -> bool {
// UTC-12 to UTC+14.
offset >= -50400 && offset <= 43200
(-50400..=43200).contains(&offset)
}

/// Lower bound for default values in dates.
Expand Down
2 changes: 2 additions & 0 deletions below/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![deny(clippy::all)]

pub mod cliutil;
pub mod dateutil;
pub mod fileutil;
Expand Down
10 changes: 2 additions & 8 deletions below/common/src/logutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ where
TargetLog::All => {
let term_res = self.1.lock().unwrap().write(buf);
let file_res = self.0.lock().unwrap().write(buf);
if let Err(e) = term_res {
return Err(e);
}

term_res?;
file_res
}
TargetLog::File => self.0.lock().unwrap().write(buf),
Expand All @@ -176,10 +173,7 @@ where
fn flush(&mut self) -> io::Result<()> {
let term_res = self.1.lock().unwrap().flush();
let file_res = self.0.lock().unwrap().flush();
if let Err(e) = term_res {
return Err(e);
}

term_res?;
file_res
}
}
Expand Down
2 changes: 1 addition & 1 deletion below/common/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ where

/// Convert system timestamp to human readable datetime.
pub fn timestamp_to_datetime(timestamp: &i64) -> String {
let naive = NaiveDateTime::from_timestamp_opt(timestamp.clone(), 0).unwrap();
let naive = NaiveDateTime::from_timestamp_opt(*timestamp, 0).unwrap();
let datetime = naive.and_utc();
datetime
.with_timezone(&Local)
Expand Down

0 comments on commit b2cbe18

Please sign in to comment.