Skip to content

Commit

Permalink
Add clippy lints for panics (#1650)
Browse files Browse the repository at this point in the history
See #1363
  • Loading branch information
ozghimire authored Mar 24, 2022
1 parent 5bd67c8 commit 9d86b99
Show file tree
Hide file tree
Showing 105 changed files with 636 additions and 25 deletions.
1 change: 1 addition & 0 deletions components/calendar/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ pub mod week_of {
min_week_days: 1,
};

#[allow(clippy::expect_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
week_of(
&calendar,
// The duration of the current and previous unit does not influence the result if min_week_days = 1
Expand Down
2 changes: 2 additions & 0 deletions components/calendar/src/coptic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl Coptic {
}

fn fixed_from_coptic_integers(year: i32, month: i32, day: i32) -> i32 {
#[allow(clippy::unwrap_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
Self::fixed_from_coptic(ArithmeticDate {
year,
month: month.try_into().unwrap(),
Expand All @@ -155,6 +156,7 @@ impl Coptic {
let month = (date - Self::fixed_from_coptic_integers(year, 1, 1)) / 30 + 1;
let day = date + 1 - Self::fixed_from_coptic_integers(year, month, 1);

#[allow(clippy::unwrap_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
*Date::new_coptic_date_from_integers(year, month as u8, day as u8)
.unwrap()
.inner()
Expand Down
3 changes: 3 additions & 0 deletions components/calendar/src/iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ impl Iso {
}

fn fixed_from_iso_integers(year: i32, month: i32, day: i32) -> i32 {
#[allow(clippy::unwrap_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
Self::fixed_from_iso(
*Date::new_iso_date_from_integers(year, month as u8, day as u8)
.unwrap()
Expand Down Expand Up @@ -444,13 +445,15 @@ impl Iso {
};
let month = (12 * (prior_days + correction) + 373) / 367;
let day = date - Self::fixed_from_iso_integers(year, month, 1) + 1;
#[allow(clippy::unwrap_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
Date::new_iso_date_from_integers(year, month as u8, day as u8).unwrap()
}

pub(crate) fn day_of_year(date: IsoDateInner) -> u32 {
// Cumulatively how much are dates in each month
// offset from "30 days in each month" (in non leap years)
let month_offset = [0, 1, -1, 0, 0, 1, 1, 2, 3, 3, 4, 4];
#[allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.
let mut offset = month_offset[date.month.0 as usize - 1];
if Self::is_leap_year(date.year) && date.month.0 > 2 {
// Months after February in a leap year are offset by one less
Expand Down
2 changes: 2 additions & 0 deletions components/calendar/src/julian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ impl Julian {
}

pub(crate) fn fixed_from_julian_integers(year: i32, month: i32, day: i32) -> i32 {
#[allow(clippy::unwrap_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
Self::fixed_from_julian(ArithmeticDate {
year,
month: month.try_into().unwrap(),
Expand Down Expand Up @@ -187,6 +188,7 @@ impl Julian {
let month = (12 * (prior_days + correction) + 373) / 367;
let day = date - Self::fixed_from_julian_integers(year, month, 1) + 1;

#[allow(clippy::unwrap_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
*Date::new_julian_date_from_integers(year, month as u8, day as u8)
.unwrap()
.inner()
Expand Down
11 changes: 10 additions & 1 deletion components/calendar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![cfg_attr(
not(any(test, feature = "std")),
no_std,
deny(
clippy::indexing_slicing,
clippy::unwrap_used,
clippy::expect_used,
clippy::panic
)
)]

//! The `icu_calendar` crate contains the core types used by ICU4X for dealing
//! with dates, times, and custom calendars.
Expand Down
4 changes: 4 additions & 0 deletions components/calendar/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//!
//! Read more about data providers: [`icu_provider`]
#![allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.

use core::str::FromStr;
use icu_provider::{yoke, zerofrom};
use tinystr::TinyStr16;
Expand Down Expand Up @@ -41,7 +43,9 @@ impl FromStr for EraStartDate {
type Err = ();
fn from_str(mut s: &str) -> Result<Self, ()> {
let mut sign = 1;
#[allow(clippy::indexing_slicing)]
if s.starts_with('-') {
// TODO(#1668) Clippy exceptions need docs or fixing.
s = &s[1..];
sign = -1;
}
Expand Down
11 changes: 11 additions & 0 deletions components/calendar/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,32 @@ impl FromStr for GmtOffset {
let seconds = match input.chars().count() {
/* ±hh */
3 => {
#[allow(clippy::indexing_slicing)]
// TODO(#1668) Clippy exceptions need docs or fixing.
let hour: u8 = input[1..3].parse()?;
offset_sign * (hour as i32 * 60 * 60)
}
/* ±hhmm */
5 => {
#[allow(clippy::indexing_slicing)]
// TODO(#1668) Clippy exceptions need docs or fixing.
let hour: u8 = input[1..3].parse()?;
#[allow(clippy::indexing_slicing)]
// TODO(#1668) Clippy exceptions need docs or fixing.
let minute: u8 = input[3..5].parse()?;
offset_sign * (hour as i32 * 60 * 60 + minute as i32 * 60)
}
/* ±hh:mm */
6 => {
#[allow(clippy::indexing_slicing)]
// TODO(#1668) Clippy exceptions need docs or fixing.
let hour: u8 = input[1..3].parse()?;
#[allow(clippy::indexing_slicing)]
// TODO(#1668) Clippy exceptions need docs or fixing.
let minute: u8 = input[4..6].parse()?;
offset_sign * (hour as i32 * 60 * 60 + minute as i32 * 60)
}
#[allow(clippy::panic)] // TODO(#1668) Clippy exceptions need docs or fixing.
_ => panic!("Invalid time-zone designator"),
};

Expand Down
8 changes: 8 additions & 0 deletions components/datetime/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,15 @@ impl<'data, T: DateTimeInput> LocalizedDateTimeInput<T> for DateTimeInputWithLoc
fn year_week(&self) -> Result<Year, DateTimeError> {
year_week(
self.data,
#[allow(clippy::expect_used)]
// TODO(#1668) Clippy exceptions need docs or fixing.
self.calendar
.expect("calendar must be provided when using week of methods"),
)
}

fn week_of_month(&self) -> Result<WeekOfMonth, DateTimeError> {
#[allow(clippy::expect_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
week_of_month(
self.data,
self.calendar
Expand All @@ -241,6 +244,8 @@ impl<'data, T: DateTimeInput> LocalizedDateTimeInput<T> for DateTimeInputWithLoc
fn week_of_year(&self) -> Result<WeekOfYear, DateTimeError> {
week_of_year(
self.data,
#[allow(clippy::expect_used)]
// TODO(#1668) Clippy exceptions need docs or fixing.
self.calendar
.expect("calendar must be provided when using week of methods"),
)
Expand All @@ -261,12 +266,14 @@ impl<'data, T: ZonedDateTimeInput> LocalizedDateTimeInput<T>
fn year_week(&self) -> Result<Year, DateTimeError> {
year_week(
self.data,
#[allow(clippy::expect_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
self.calendar
.expect("calendar must be provided when using week of methods"),
)
}

fn week_of_month(&self) -> Result<WeekOfMonth, DateTimeError> {
#[allow(clippy::expect_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
week_of_month(
self.data,
self.calendar
Expand All @@ -278,6 +285,7 @@ impl<'data, T: ZonedDateTimeInput> LocalizedDateTimeInput<T>
fn week_of_year(&self) -> Result<WeekOfYear, DateTimeError> {
week_of_year(
self.data,
#[allow(clippy::expect_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
self.calendar
.expect("calendar must be provided when using week of methods"),
)
Expand Down
3 changes: 3 additions & 0 deletions components/datetime/src/fields/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ impl AsULE for fields::Field {
#[inline]
fn from_unaligned(unaligned: Self::ULE) -> Self {
let value = unaligned.0;
#[allow(clippy::unwrap_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
let symbol = fields::FieldSymbol::from_idx(value[0]).unwrap();
#[allow(clippy::unwrap_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
let length = fields::FieldLength::from_idx(value[1]).unwrap();
fields::Field { symbol, length }
}
Expand All @@ -73,6 +75,7 @@ unsafe impl ULE for FieldULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
let mut chunks = bytes.chunks_exact(2);

#[allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.
if !chunks.all(|c| fields::Field::bytes_in_range(&c[0], &c[1])) {
return Err(ZeroVecError::parse::<Self>());
}
Expand Down
6 changes: 6 additions & 0 deletions components/datetime/src/format/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ where
} else {
let buffer = num.to_string();
let len = buffer.len();
#[allow(clippy::indexing_slicing)]
// TODO(#1668) Clippy exceptions need docs or fixing.
result.write_str(&buffer[len - 2..])
}
}
Expand Down Expand Up @@ -165,6 +167,7 @@ where
{
match field.symbol {
FieldSymbol::Era => {
#[allow(clippy::expect_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
let symbol = symbols
.expect("Expect symbols to be present")
.get_symbol_for_era(
Expand Down Expand Up @@ -200,6 +203,7 @@ where
field.length,
)?,
length => {
#[allow(clippy::expect_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
let symbol = symbols
.expect("Expect symbols to be present")
.get_symbol_for_month(
Expand Down Expand Up @@ -228,6 +232,7 @@ where
.datetime()
.iso_weekday()
.ok_or(Error::MissingInputField)?;
#[allow(clippy::expect_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
let symbol = symbols
.expect("Expect symbols to be present")
.get_symbol_for_weekday(weekday, field.length, dow)?;
Expand Down Expand Up @@ -287,6 +292,7 @@ where
field.length,
)?,
FieldSymbol::DayPeriod(period) => {
#[allow(clippy::expect_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
let symbol = symbols
.expect("Expect symbols to be present")
.get_symbol_for_day_period(
Expand Down
11 changes: 10 additions & 1 deletion components/datetime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@
//! [`Length`]: options::length
//! [`DateTime`]: icu_calendar::DateTime
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![cfg_attr(
not(any(test, feature = "std")),
no_std,
deny(
clippy::indexing_slicing,
clippy::unwrap_used,
clippy::expect_used,
clippy::panic
)
)]
extern crate alloc;

mod calendar;
Expand Down
6 changes: 6 additions & 0 deletions components/datetime/src/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ pub mod zoned_datetime;
/// .expect("Failed to parse a datetime.");
/// ```
pub fn parse_gregorian_from_str(input: &str) -> Result<DateTime<Gregorian>, DateTimeError> {
#[allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.
let year: i32 = input[0..4].parse()?;
#[allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.
let month: u8 = input[5..7].parse()?;
#[allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.
let day: u8 = input[8..10].parse()?;
#[allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.
let hour: u8 = input[11..13].parse()?;
#[allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.
let minute: u8 = input[14..16].parse()?;
#[allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.
let second: u8 = input[17..19].parse()?;
DateTime::new_gregorian_datetime_from_integers(year, month, day, hour, minute, second)
}
1 change: 1 addition & 0 deletions components/datetime/src/mock/zoned_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl FromStr for MockZonedDateTime {
let time_zone = match input
.rfind(|c| c == '+' || /* ASCII */ c == '-' || /* U+2212 */ c == '−' || c == 'Z')
{
#[allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.
Some(index) => MockTimeZone::from_str(&input[index..])?,
None => return Err(DateTimeError::InvalidTimeZoneOffset),
};
Expand Down
8 changes: 7 additions & 1 deletion components/datetime/src/options/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,13 @@ impl Bag {
}

debug_assert!(
fields.windows(2).all(|f| f[0] < f[1]),
fields.windows(2).all(|f| {
#[allow(clippy::indexing_slicing)]
// TODO(#1668) Clippy exceptions need docs or fixing.
// Change to code redired as clippy on statements arex experimental
let comp = f[0] < f[1];
comp
}),
"The fields are sorted and unique."
);

Expand Down
2 changes: 2 additions & 0 deletions components/datetime/src/pattern/common/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ mod runtime {
E: de::Error,
{
// Parse a string into a list of fields.
#[allow(clippy::expect_used)]
// TODO(#1668) Clippy exceptions need docs or fixing.
let pattern = pattern_string.parse().expect("Failed to parse pattern");
Ok(GenericPattern::from(&pattern))
}
Expand Down
11 changes: 10 additions & 1 deletion components/datetime/src/pattern/item/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ unsafe impl ULE for PatternItemULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
let mut chunks = bytes.chunks_exact(3);

#[allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.
if !chunks.all(|c| Self::bytes_in_range((&c[0], &c[1], &c[2]))) {
return Err(ZeroVecError::parse::<Self>());
}
Expand Down Expand Up @@ -130,10 +131,16 @@ impl AsULE for PatternItem {
match PatternItemULE::determine_field_from_u8(value[0]) {
false => {
let u = u32::from_be_bytes([0x00, value[0], value[1], value[2]]);
#[allow(clippy::unwrap_used)]
// TODO(#1668) Clippy exceptions need docs or fixing.
PatternItem::Literal(char::try_from(u).unwrap())
}
true => {
#[allow(clippy::unwrap_used)]
// TODO(#1668) Clippy exceptions need docs or fixing.
let symbol = fields::FieldSymbol::from_idx(value[1]).unwrap();
#[allow(clippy::unwrap_used)]
// TODO(#1668) Clippy exceptions need docs or fixing.
let length = fields::FieldLength::from_idx(value[2]).unwrap();
let field = fields::Field { symbol, length };
PatternItem::Field(field)
Expand Down Expand Up @@ -230,7 +237,7 @@ impl GenericPatternItemULE {
unsafe impl ULE for GenericPatternItemULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
let mut chunks = bytes.chunks_exact(3);

#[allow(clippy::indexing_slicing)] // TODO(#1668) Clippy exceptions need docs or fixing.
if !chunks.all(|c| Self::bytes_in_range((&c[0], &c[1], &c[2]))) {
return Err(ZeroVecError::parse::<Self>());
}
Expand Down Expand Up @@ -263,6 +270,8 @@ impl AsULE for GenericPatternItem {
match GenericPatternItemULE::determine_field_from_u8(value[0]) {
false => {
let u = u32::from_be_bytes([0x00, value[0], value[1], value[2]]);
#[allow(clippy::unwrap_used)]
// TODO(#1668) Clippy exceptions need docs or fixing.
Self::Literal(char::try_from(u).unwrap())
}
true => Self::Placeholder(value[2]),
Expand Down
2 changes: 2 additions & 0 deletions components/datetime/src/pattern/reference/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ impl fmt::Display for GenericPattern {
GenericPatternItem::Placeholder(idx) => {
dump_buffer_into_formatter(&buffer, formatter)?;
buffer.clear();
#[allow(clippy::expect_used)]
// TODO(#1668) Clippy exceptions need docs or fixing.
let idx = char::from_digit(*idx as u32, 10)
.expect("Failed to convert placeholder idx to char");
formatter.write_char('{')?;
Expand Down
2 changes: 2 additions & 0 deletions components/datetime/src/pattern/reference/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ impl GenericPattern {
match item {
GenericPatternItem::Placeholder(idx) => {
let replacement = replacements.get(idx as usize).ok_or_else(|| {
#[allow(clippy::expect_used)]
// TODO(#1668) Clippy exceptions need docs or fixing.
let idx = char::from_digit(idx as u32, 10)
.expect("Failed to convert placeholder idx to char");
PatternError::UnknownSubstitution(idx)
Expand Down
2 changes: 2 additions & 0 deletions components/datetime/src/pattern/reference/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl<'p> Parser<'p> {
(Segment::Literal { ref mut quoted, .. }, false) => {
*quoted = !*quoted;
}
#[allow(clippy::panic)] // TODO(#1668) Clippy exceptions need docs or fixing.
_ => panic!(),
}
Ok(true)
Expand Down Expand Up @@ -150,6 +151,7 @@ impl<'p> Parser<'p> {
}
}
}
#[allow(clippy::panic)] // TODO(#1668) Clippy exceptions need docs or fixing.
_ => panic!(),
}
Ok(())
Expand Down
2 changes: 2 additions & 0 deletions components/datetime/src/pattern/runtime/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ impl<'data> GenericPattern<'data> {
result.extend(date.items.iter());
}
GenericPatternItem::Placeholder(idx) => {
#[allow(clippy::expect_used)]
// TODO(#1668) Clippy exceptions need docs or fixing.
let idx = char::from_digit(idx as u32, 10)
.expect("Failed to convert placeholder idx to char");
return Err(PatternError::UnknownSubstitution(idx));
Expand Down
Loading

0 comments on commit 9d86b99

Please sign in to comment.