ソースを参照

Auto merge of #294 - neosilky:clippy-fixes, r=SimonSapin

Fix up some issues flagged by clippy

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/294)
<!-- Reviewable:end -->
bors-servo 9 年 前
コミット
e461e347cc
7 ファイル変更25 行追加27 行削除
  1. 3 3
      fuzz/fuzzers/parse.rs
  2. 1 1
      src/form_urlencoded.rs
  3. 6 8
      src/host.rs
  4. 9 9
      src/parser.rs
  5. 2 2
      src/percent_encoding.rs
  6. 2 2
      tests/data.rs
  7. 2 2
      tests/unit.rs

+ 3 - 3
fuzz/fuzzers/parse.rs

@@ -4,7 +4,7 @@ extern crate url;
 use std::str;
 
 fuzz_target!(|data: &[u8]| {
-	if let Ok(utf8) = str::from_utf8(data) {
-		let url = url::Url::parse(utf8);
-	}
+    if let Ok(utf8) = str::from_utf8(data) {
+        let _ = url::Url::parse(utf8);
+    }
 });

+ 1 - 1
src/form_urlencoded.rs

@@ -121,7 +121,7 @@ fn decode(input: &[u8], encoding: EncodingOverride) -> Cow<str> {
 }
 
 /// Replace b'+' with b' '
-fn replace_plus<'a>(input: &'a [u8]) -> Cow<'a, [u8]> {
+fn replace_plus(input: &[u8]) -> Cow<[u8]> {
     match input.iter().position(|&b| b == b'+') {
         None => Cow::Borrowed(input),
         Some(first_position) => {

+ 6 - 8
src/host.rs

@@ -139,8 +139,8 @@ impl Host<String> {
     ///
     /// https://url.spec.whatwg.org/#host-parsing
     pub fn parse(input: &str) -> Result<Self, ParseError> {
-        if input.starts_with("[") {
-            if !input.ends_with("]") {
+        if input.starts_with('[') {
+            if !input.ends_with(']') {
                 return Err(ParseError::InvalidIpv6Address)
             }
             return parse_ipv6addr(&input[1..input.len() - 1]).map(Host::Ipv6)
@@ -304,17 +304,17 @@ fn parse_ipv4number(mut input: &str) -> Result<u32, ()> {
     if input.starts_with("0x") || input.starts_with("0X") {
         input = &input[2..];
         r = 16;
-    } else if input.len() >= 2 && input.starts_with("0") {
+    } else if input.len() >= 2 && input.starts_with('0') {
         input = &input[1..];
         r = 8;
     }
     if input.is_empty() {
         return Ok(0);
     }
-    if input.starts_with("+") {
+    if input.starts_with('+') {
         return Err(())
     }
-    match u32::from_str_radix(&input, r) {
+    match u32::from_str_radix(input, r) {
         Ok(number) => Ok(number),
         Err(_) => Err(()),
     }
@@ -477,9 +477,7 @@ fn parse_ipv6addr(input: &str) -> ParseResult<Ipv6Addr> {
             let mut swaps = piece_pointer - compress_pointer;
             piece_pointer = 7;
             while swaps > 0 {
-                let tmp = pieces[piece_pointer];
-                pieces[piece_pointer] = pieces[compress_pointer + swaps - 1];
-                pieces[compress_pointer + swaps - 1] = tmp;
+                pieces.swap(piece_pointer, compress_pointer + swaps - 1);
                 swaps -= 1;
                 piece_pointer -= 1;
             }

+ 9 - 9
src/parser.rs

@@ -209,7 +209,7 @@ impl<F: FnMut(char) -> bool> Pattern for F {
 impl<'i> Iterator for Input<'i> {
     type Item = char;
     fn next(&mut self) -> Option<char> {
-        self.chars.by_ref().filter(|&c| !matches!(c, '\t' | '\n' | '\r')).next()
+        self.chars.by_ref().find(|&c| !matches!(c, '\t' | '\n' | '\r'))
     }
 }
 
@@ -736,8 +736,8 @@ impl<'a> Parser<'a> {
         Ok((host_end, host.into(), port, remaining))
     }
 
-    pub fn parse_host<'i>(mut input: Input<'i>, scheme_type: SchemeType)
-                             -> ParseResult<(Host<String>, Input<'i>)> {
+    pub fn parse_host(mut input: Input, scheme_type: SchemeType)
+                             -> ParseResult<(Host<String>, Input)> {
         // Undo the Input abstraction here to avoid allocating in the common case
         // where the host part of the input does not contain any tab or newline
         let input_str = input.chars.as_str();
@@ -830,9 +830,9 @@ impl<'a> Parser<'a> {
         Ok((true, host, remaining))
     }
 
-    pub fn parse_port<'i, P>(mut input: Input<'i>, default_port: P,
+    pub fn parse_port<P>(mut input: Input, default_port: P,
                                 context: Context)
-                                -> ParseResult<(Option<u16>, Input<'i>)>
+                                -> ParseResult<(Option<u16>, Input)>
                                 where P: Fn() -> Option<u16> {
         let mut port: u32 = 0;
         let mut has_any_digit = false;
@@ -854,7 +854,7 @@ impl<'a> Parser<'a> {
         if !has_any_digit || opt_port == default_port() {
             opt_port = None;
         }
-        return Ok((opt_port, input))
+        Ok((opt_port, input))
     }
 
     pub fn parse_path_start<'i>(&mut self, scheme_type: SchemeType, has_host: &mut bool,
@@ -878,7 +878,7 @@ impl<'a> Parser<'a> {
                           path_start: usize, mut input: Input<'i>)
                           -> Input<'i> {
         // Relative path state
-        debug_assert!(self.serialization.ends_with("/"));
+        debug_assert!(self.serialization.ends_with('/'));
         loop {
             let segment_start = self.serialization.len();
             let mut ends_with_slash = false;
@@ -926,7 +926,7 @@ impl<'a> Parser<'a> {
                     debug_assert!(self.serialization.as_bytes()[segment_start - 1] == b'/');
                     self.serialization.truncate(segment_start - 1);  // Truncate "/.."
                     self.pop_path(scheme_type, path_start);
-                    if !self.serialization[path_start..].ends_with("/") {
+                    if !self.serialization[path_start..].ends_with('/') {
                         self.serialization.push('/')
                     }
                 },
@@ -1030,7 +1030,7 @@ impl<'a> Parser<'a> {
                 }
             }
             None => return Ok((None, None)),
-            _ => panic!("Programming error. parse_query_and_fragment() called without ? or # {:?}")
+            _ => panic!("Programming error. parse_query_and_fragment() called without ? or #")
         }
 
         let fragment_start = to_u32(self.serialization.len())?;

+ 2 - 2
src/percent_encoding.rs

@@ -242,7 +242,7 @@ impl<'a, E: EncodeSet> From<PercentEncode<'a, E>> for Cow<'a, str> {
 /// (which returns `Cow::Borrowed` when `input` contains no percent-encoded sequence)
 /// and has `decode_utf8()` and `decode_utf8_lossy()` methods.
 #[inline]
-pub fn percent_decode<'a>(input: &'a [u8]) -> PercentDecode<'a> {
+pub fn percent_decode(input: &[u8]) -> PercentDecode {
     PercentDecode {
         bytes: input.iter()
     }
@@ -298,7 +298,7 @@ impl<'a> PercentDecode<'a> {
     /// If the percent-decoding is different from the input, return it as a new bytes vector.
     pub fn if_any(&self) -> Option<Vec<u8>> {
         let mut bytes_iter = self.bytes.clone();
-        while bytes_iter.find(|&&b| b == b'%').is_some() {
+        while bytes_iter.any(|&b| b == b'%') {
             if let Some(decoded_byte) = after_percent_sign(&mut bytes_iter) {
                 let initial_bytes = self.bytes.as_slice();
                 let unchanged_bytes_len = initial_bytes.len() - bytes_iter.len() - 3;

+ 2 - 2
tests/data.rs

@@ -26,7 +26,7 @@ fn check_invariants(url: &Url) {
 }
 
 
-fn run_parsing(input: String, base: String, expected: Result<ExpectedAttributes, ()>) {
+fn run_parsing(input: &str, base: &str, expected: Result<ExpectedAttributes, ()>) {
     let base = match Url::parse(&base) {
         Ok(base) => base,
         Err(message) => panic!("Error parsing base {:?}: {}", base, message)
@@ -135,7 +135,7 @@ fn collect_parsing<F: FnMut(String, test::TestFn)>(add_test: &mut F) {
             })
         };
         add_test(format!("{:?} @ base {:?}", input, base),
-                 test::TestFn::dyn_test_fn(move || run_parsing(input, base, expected)));
+                 test::TestFn::dyn_test_fn(move || run_parsing(&input, &base, expected)));
     }
 }
 

+ 2 - 2
tests/unit.rs

@@ -164,12 +164,12 @@ fn test_equality() {
     // Different scheme
     let a: Url = url("http://example.com/");
     let b: Url = url("https://example.com/");
-    assert!(a != b);
+    assert_ne!(a, b);
 
     // Different host
     let a: Url = url("http://foo.com/");
     let b: Url = url("http://bar.com/");
-    assert!(a != b);
+    assert_ne!(a, b);
 
     // Missing path, automatically substituted. Semantically the same.
     let a: Url = url("http://foo.com");