Skip to content

Commit

Permalink
Address clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
hsivonen committed May 23, 2024
1 parent c8a4bd3 commit be3db8e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 69 deletions.
2 changes: 2 additions & 0 deletions idna/benches/all.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

#[macro_use]
extern crate bencher;
extern crate idna;
Expand Down
10 changes: 3 additions & 7 deletions idna/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ compile_error!("the `alloc` feature must be enabled");
#[cfg(not(feature = "compiled_data"))]
compile_error!("the `compiled_data` feature must be enabled");

#[cfg(test)]
#[macro_use]
extern crate assert_matches;

use alloc::borrow::Cow;
use alloc::string::String;
pub use uts46::AsciiDenyList;
Expand Down Expand Up @@ -103,10 +99,10 @@ impl core::fmt::Display for Errors {
/// If you have a `&str` instead of `&[u8]`, just call `.to_bytes()` on it before
/// passing it to this function. It's still preferable to use this function over
/// the sibling functions that take `&str`.
pub fn domain_to_ascii_cow<'a>(
domain: &'a [u8],
pub fn domain_to_ascii_cow(
domain: &[u8],
ascii_deny_list: AsciiDenyList,
) -> Result<Cow<'a, str>, Errors> {
) -> Result<Cow<'_, str>, Errors> {
Uts46::new().to_ascii(
domain,
ascii_deny_list,
Expand Down
116 changes: 55 additions & 61 deletions idna/src/uts46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ fn apply_ascii_deny_list_to_potentially_upper_case_ascii(b: u8, deny_list: u128)
if in_inclusive_range8(b, b'A', b'Z') {
return char::from(b + 0x20);
}
return '\u{FFFD}';
'\u{FFFD}'
}

#[inline(always)]
Expand Down Expand Up @@ -573,10 +573,16 @@ pub struct Uts46 {
joining_type: CodePointMapDataBorrowed<'static, JoiningType>,
}

impl Uts46 {
// XXX Should this be behind a `compiled_data` feature?
#[cfg(feature = "compiled_data")]
impl Default for Uts46 {
fn default() -> Self {
Self::new()

Check warning on line 579 in idna/src/uts46.rs

View check run for this annotation

Codecov / codecov/patch

idna/src/uts46.rs#L578-L579

Added lines #L578 - L579 were not covered by tests
}
}

impl Uts46 {
/// Constructor using data compiled into the binary.
#[cfg(feature = "compiled_data")]
pub const fn new() -> Self {
Self {
mapper: Uts46Mapper::new(),
Expand Down Expand Up @@ -832,6 +838,7 @@ impl Uts46 {
/// length limits on Punycode treating especially long inputs as being in error. These
/// limits are well higher than the DNS length limits and are not more restrictive than
/// the limits imposed by ICU4C.
#[allow(clippy::too_many_arguments)]
pub fn process<W: Write + ?Sized, OutputUnicode: FnMut(&[char], &[char], bool) -> bool>(
&self,
domain_name: &[u8],
Expand Down Expand Up @@ -912,16 +919,14 @@ impl Uts46 {
for c in tail.iter() {
sink.write_char(char::from(c.to_ascii_lowercase()))?;
}
} else if flushed_prefix {
// SAFETY: `mixed_case` is known to be ASCII.
sink.write_str(unsafe { core::str::from_utf8_unchecked(mixed_case) })?;
} else {
if flushed_prefix {
// SAFETY: `mixed_case` is known to be ASCII.
sink.write_str(unsafe { core::str::from_utf8_unchecked(mixed_case) })?;
} else {
passthrough_up_to_extended += mixed_case.len();
if passthrough_up_to_extended == domain_name.len() {
debug_assert!(!had_errors);
return Ok(ProcessingSuccess::Passthrough);
}
passthrough_up_to_extended += mixed_case.len();
if passthrough_up_to_extended == domain_name.len() {
debug_assert!(!had_errors);
return Ok(ProcessingSuccess::Passthrough);
}
}
continue;

Check warning on line 932 in idna/src/uts46.rs

View check run for this annotation

Codecov / codecov/patch

idna/src/uts46.rs#L932

Added line #L932 was not covered by tests
Expand Down Expand Up @@ -969,16 +974,14 @@ impl Uts46 {
for c in tail.iter() {
sink.write_char(char::from(c.to_ascii_lowercase()))?;
}
} else if flushed_prefix {
// SAFETY: `mixed_case` is known to be ASCII.
sink.write_str(unsafe { core::str::from_utf8_unchecked(mixed_case) })?;
} else {
if flushed_prefix {
// SAFETY: `mixed_case` is known to be ASCII.
sink.write_str(unsafe { core::str::from_utf8_unchecked(mixed_case) })?;
} else {
passthrough_up_to_extended += mixed_case.len();
if passthrough_up_to_extended == domain_name.len() {
debug_assert!(!had_errors);
return Ok(ProcessingSuccess::Passthrough);
}
passthrough_up_to_extended += mixed_case.len();
if passthrough_up_to_extended == domain_name.len() {
debug_assert!(!had_errors);
return Ok(ProcessingSuccess::Passthrough);
}
}
} else {
Expand Down Expand Up @@ -1037,15 +1040,11 @@ impl Uts46 {
for c in tail.iter() {
sink.write_char(char::from(c.to_ascii_lowercase()))?;
}
} else if flushed_prefix {
// SAFETY: `mixed_case` is known to be ASCII.
sink.write_str(unsafe { core::str::from_utf8_unchecked(mixed_case) })?;
} else {
if flushed_prefix {
// SAFETY: `mixed_case` is known to be ASCII.
sink.write_str(unsafe {
core::str::from_utf8_unchecked(mixed_case)
})?;
} else {
passthrough_up_to_extended += mixed_case.len();
}
passthrough_up_to_extended += mixed_case.len();
}
continue;

Check warning on line 1049 in idna/src/uts46.rs

View check run for this annotation

Codecov / codecov/patch

idna/src/uts46.rs#L1049

Added line #L1049 was not covered by tests
}
Expand Down Expand Up @@ -1084,15 +1083,11 @@ impl Uts46 {
for c in tail.iter() {
sink.write_char(char::from(c.to_ascii_lowercase()))?;
}
} else if flushed_prefix {
// SAFETY: `mixed_case` is known to be ASCII.
sink.write_str(unsafe { core::str::from_utf8_unchecked(mixed_case) })?;
} else {
if flushed_prefix {
// SAFETY: `mixed_case` is known to be ASCII.
sink.write_str(unsafe {
core::str::from_utf8_unchecked(mixed_case)
})?;
} else {
passthrough_up_to_extended += mixed_case.len();
}
passthrough_up_to_extended += mixed_case.len();
}
} else {
if !flushed_prefix {
Expand Down Expand Up @@ -1174,17 +1169,15 @@ impl Uts46 {
// an ASCII fast path that bypasses the normalizer for ASCII
// after a pre-existing ASCII dot (pre-existing in the sense
// of not coming from e.g. normalizing an ideographic dot).
if in_prefix {
if is_passthrough_ascii_label(label) {
if seen_label {
debug_assert_eq!(domain_name[passthrough_up_to], b'.');
passthrough_up_to += 1;
}
seen_label = true;

passthrough_up_to += label.len();
continue;
if in_prefix && is_passthrough_ascii_label(label) {
if seen_label {
debug_assert_eq!(domain_name[passthrough_up_to], b'.');
passthrough_up_to += 1;
}
seen_label = true;

passthrough_up_to += label.len();
continue;

Check warning on line 1180 in idna/src/uts46.rs

View check run for this annotation

Codecov / codecov/patch

idna/src/uts46.rs#L1180

Added line #L1180 was not covered by tests
}
if seen_label {
if in_prefix {
Expand Down Expand Up @@ -1281,15 +1274,15 @@ impl Uts46 {
domain_buffer.push(c);
}
if non_punycode_ascii_label {
if hyphens != Hyphens::Allow {
if check_hyphens(
if hyphens != Hyphens::Allow
&& check_hyphens(
&mut domain_buffer[current_label_start..],
hyphens == Hyphens::CheckFirstLast,
fail_fast,
&mut had_errors,

Check warning on line 1282 in idna/src/uts46.rs

View check run for this annotation

Codecov / codecov/patch

idna/src/uts46.rs#L1281-L1282

Added lines #L1281 - L1282 were not covered by tests
) {
return (0, false, true);
}
)
{
return (0, false, true);
}
already_punycode.push(if had_errors {
AlreadyAsciiLabel::Other
Expand Down Expand Up @@ -1408,7 +1401,7 @@ impl Uts46 {
return (0, false, true);
}

if n == None {
if n.is_none() {
break;

Check warning on line 1405 in idna/src/uts46.rs

View check run for this annotation

Codecov / codecov/patch

idna/src/uts46.rs#L1405

Added line #L1405 was not covered by tests
}
domain_buffer.push('.');
Expand All @@ -1434,7 +1427,7 @@ impl Uts46 {
}
}

let is_bidi = self.is_bidi(&domain_buffer);
let is_bidi = self.is_bidi(domain_buffer);
if is_bidi {
for label in domain_buffer.split_mut(|c| *c == '.') {
if let Some((first, tail)) = label.split_first_mut() {
Expand All @@ -1451,6 +1444,7 @@ impl Uts46 {
let is_ltr = first_bc == BidiClass::LeftToRight;
// Trim NSM
let mut middle = tail;
#[allow(clippy::while_let_loop)]
loop {
if let Some((last, prior)) = middle.split_last_mut() {
let last_bc = self.bidi_class.get(*last);
Expand Down Expand Up @@ -1583,7 +1577,7 @@ impl Uts46 {
})
{
if fail_fast {
true;
return true;
}
*had_errors = true;
}
Expand All @@ -1600,15 +1594,15 @@ impl Uts46 {
first_needs_combining_mark_check: bool,
needs_contextj_check: bool,
) -> bool {
if hyphens != Hyphens::Allow {
if check_hyphens(
if hyphens != Hyphens::Allow
&& check_hyphens(
mut_label,

Check warning on line 1599 in idna/src/uts46.rs

View check run for this annotation

Codecov / codecov/patch

idna/src/uts46.rs#L1599

Added line #L1599 was not covered by tests
hyphens == Hyphens::CheckFirstLast,
fail_fast,
had_errors,

Check warning on line 1602 in idna/src/uts46.rs

View check run for this annotation

Codecov / codecov/patch

idna/src/uts46.rs#L1601-L1602

Added lines #L1601 - L1602 were not covered by tests
) {
return true;
}
)
{
return true;
}
if first_needs_combining_mark_check {
if let Some(first) = mut_label.first_mut() {
Expand Down Expand Up @@ -1675,7 +1669,7 @@ impl Uts46 {
}
}

if !is_ascii(&mut_label) && mut_label.len() > PUNYCODE_ENCODE_MAX_INPUT_LENGTH {
if !is_ascii(mut_label) && mut_label.len() > PUNYCODE_ENCODE_MAX_INPUT_LENGTH {
// Limit quadratic behavior
// https://github.com/whatwg/url/issues/824
// https://unicode-org.atlassian.net/browse/ICU-13727
Expand Down
3 changes: 3 additions & 0 deletions idna/tests/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(clippy::assigning_clones)]
#![allow(deprecated)]

use crate::test::TestFn;
use std::char;
use std::fmt::Write;
Expand Down
1 change: 0 additions & 1 deletion idna/tests/unitbis.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use assert_matches::assert_matches;
use idna::uts46::AsciiDenyList;
use idna::uts46::DnsLength;
use idna::uts46::Hyphens;
Expand Down
2 changes: 2 additions & 0 deletions idna/tests/uts46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(clippy::assigning_clones)]

use crate::test::TestFn;
use std::char;
use std::fmt::Write;
Expand Down

0 comments on commit be3db8e

Please sign in to comment.