From df64ee4fcb1e3e2927d04e6bb6d8a1c0feedc844 Mon Sep 17 00:00:00 2001 From: Winter <78392041+winterqt@users.noreply.github.com> Date: Tue, 19 Oct 2021 11:28:23 -0400 Subject: [PATCH] Fix Clippy lints (#416) --- examples/ssl_proxy.rs | 6 +++--- src/easy/handler.rs | 25 +++++++++---------------- src/error.rs | 2 -- src/version.rs | 1 - systest/build.rs | 5 ++--- tests/easy.rs | 2 +- tests/multi.rs | 10 ++++------ tests/post.rs | 2 +- tests/server/mod.rs | 14 +++++++------- 9 files changed, 27 insertions(+), 40 deletions(-) diff --git a/examples/ssl_proxy.rs b/examples/ssl_proxy.rs index 7a50b03efd..1884d6880c 100644 --- a/examples/ssl_proxy.rs +++ b/examples/ssl_proxy.rs @@ -16,9 +16,9 @@ fn main() -> Result<()> { handle.proxy(proxy_url)?; handle.proxy_port(proxy_port)?; - handle.proxy_cainfo(&cainfo)?; - handle.proxy_sslcert(&sslcert)?; - handle.proxy_sslkey(&sslkey)?; + handle.proxy_cainfo(cainfo)?; + handle.proxy_sslcert(sslcert)?; + handle.proxy_sslkey(sslkey)?; println!("ssl proxy setup done"); handle.perform()?; diff --git a/src/easy/handler.rs b/src/easy/handler.rs index a3fd4601f5..d97de7bb9d 100644 --- a/src/easy/handler.rs +++ b/src/easy/handler.rs @@ -1343,7 +1343,7 @@ impl Easy2 { /// `CURLOPT_POSTFIELDSIZE_LARGE`. pub fn post_field_size(&mut self, size: u64) -> Result<(), Error> { // Clear anything previous to ensure we don't read past a buffer - self.setopt_ptr(curl_sys::CURLOPT_POSTFIELDS, 0 as *const _)?; + self.setopt_ptr(curl_sys::CURLOPT_POSTFIELDS, ptr::null())?; self.setopt_off_t( curl_sys::CURLOPT_POSTFIELDSIZE_LARGE, size as curl_sys::curl_off_t, @@ -1738,7 +1738,7 @@ impl Easy2 { pub fn timeout(&mut self, timeout: Duration) -> Result<(), Error> { // TODO: checked arithmetic and casts // TODO: use CURLOPT_TIMEOUT if the timeout is too great - let ms = timeout.as_secs() * 1000 + (timeout.subsec_nanos() / 1_000_000) as u64; + let ms = timeout.as_secs() * 1000 + timeout.subsec_millis() as u64; self.setopt_long(curl_sys::CURLOPT_TIMEOUT_MS, ms as c_long) } @@ -1860,7 +1860,7 @@ impl Easy2 { /// By default this value is 300 seconds and corresponds to /// `CURLOPT_CONNECTTIMEOUT_MS`. pub fn connect_timeout(&mut self, timeout: Duration) -> Result<(), Error> { - let ms = timeout.as_secs() * 1000 + (timeout.subsec_nanos() / 1_000_000) as u64; + let ms = timeout.as_secs() * 1000 + timeout.subsec_millis() as u64; self.setopt_long(curl_sys::CURLOPT_CONNECTTIMEOUT_MS, ms as c_long) } @@ -2411,7 +2411,7 @@ impl Easy2 { /// By default this option is not set and corresponds to /// `CURLOPT_EXPECT_100_TIMEOUT_MS`. pub fn expect_100_timeout(&mut self, timeout: Duration) -> Result<(), Error> { - let ms = timeout.as_secs() * 1000 + (timeout.subsec_nanos() / 1_000_000) as u64; + let ms = timeout.as_secs() * 1000 + timeout.subsec_millis() as u64; self.setopt_long(curl_sys::CURLOPT_EXPECT_100_TIMEOUT_MS, ms as c_long) } @@ -2422,15 +2422,8 @@ impl Easy2 { //// This corresponds to `CURLINFO_CONDITION_UNMET` and may return an error if the /// option is not supported pub fn time_condition_unmet(&mut self) -> Result { - self.getopt_long(curl_sys::CURLINFO_CONDITION_UNMET).map( - |r| { - if r == 0 { - false - } else { - true - } - }, - ) + self.getopt_long(curl_sys::CURLINFO_CONDITION_UNMET) + .map(|r| r != 0) } /// Get the last used URL @@ -2894,7 +2887,7 @@ impl Easy2 { /// URL encodes a string `s` pub fn url_encode(&mut self, s: &[u8]) -> String { - if s.len() == 0 { + if s.is_empty() { return String::new(); } unsafe { @@ -2913,7 +2906,7 @@ impl Easy2 { /// URL decodes a string `s`, returning `None` if it fails pub fn url_decode(&mut self, s: &str) -> Vec { - if s.len() == 0 { + if s.is_empty() { return Vec::new(); } @@ -3078,7 +3071,7 @@ impl Easy2 { fn getopt_ptr(&mut self, opt: curl_sys::CURLINFO) -> Result<*const c_char, Error> { unsafe { - let mut p = 0 as *const c_char; + let mut p = ptr::null(); let rc = curl_sys::curl_easy_getinfo(self.inner.handle, opt, &mut p); self.cvt(rc)?; Ok(p) diff --git a/src/error.rs b/src/error.rs index 56f0a45edd..a138f75df6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,8 +4,6 @@ use std::fmt; use std::io; use std::str; -use curl_sys; - /// An error returned from various "easy" operations. /// /// This structure wraps a `CURLcode`. diff --git a/src/version.rs b/src/version.rs index f011830d90..8bceaec109 100644 --- a/src/version.rs +++ b/src/version.rs @@ -2,7 +2,6 @@ use std::ffi::CStr; use std::fmt; use std::str; -use curl_sys; use libc::{c_char, c_int}; /// Version information about libcurl and the capabilities that it supports. diff --git a/systest/build.rs b/systest/build.rs index feca0b3325..11fde95827 100644 --- a/systest/build.rs +++ b/systest/build.rs @@ -14,8 +14,7 @@ fn main() { let version = str::from_utf8(&version).unwrap(); let version = version .lines() - .filter(|l| !l.is_empty() && !l.starts_with("#")) - .next() + .find(|l| !l.is_empty() && !l.starts_with('#')) .and_then(|s| s.parse::().ok()) .unwrap_or(10000); println!("got version: {}", version); @@ -39,7 +38,7 @@ fn main() { "CURL" | "CURLM" | "CURLSH" | "curl_version_info_data" => s.to_string(), "curl_khtype" | "curl_khstat" | "curl_khmatch" => format!("enum {}", s), s if is_struct => format!("struct {}", s), - "sockaddr" => format!("struct sockaddr"), + "sockaddr" => "struct sockaddr".to_string(), s => s.to_string(), }); // cfg.fn_cname(|s, l| l.unwrap_or(s).to_string()); diff --git a/tests/easy.rs b/tests/easy.rs index 964ff18b00..db3d92ef8e 100644 --- a/tests/easy.rs +++ b/tests/easy.rs @@ -22,7 +22,7 @@ mod server; fn handle() -> Easy { let mut e = Easy::new(); t!(e.timeout(Duration::new(20, 0))); - return e; + e } fn sink(data: &[u8]) -> Result { diff --git a/tests/multi.rs b/tests/multi.rs index 84d6c2bc4f..0c5e701507 100644 --- a/tests/multi.rs +++ b/tests/multi.rs @@ -150,10 +150,8 @@ fn upload_lots() { while running { let n = t!(poll.poll(&mut events, cur_timeout)); - if n == 0 { - if t!(m.timeout()) == 0 { - running = false; - } + if n == 0 && t!(m.timeout()) == 0 { + running = false; } for event in events.iter() { @@ -167,10 +165,10 @@ fn upload_lots() { } else { let mut e = mio::Ready::empty(); if events.input() { - e = e | mio::Ready::readable(); + e |= mio::Ready::readable(); } if events.output() { - e = e | mio::Ready::writable(); + e |= mio::Ready::writable(); } if token == 0 { let token = next_token; diff --git a/tests/post.rs b/tests/post.rs index 541a35128c..15e2df7d3b 100644 --- a/tests/post.rs +++ b/tests/post.rs @@ -20,7 +20,7 @@ fn handle() -> Easy { let mut list = List::new(); t!(list.append("Expect:")); t!(e.http_headers(list)); - return e; + e } #[test] diff --git a/tests/server/mod.rs b/tests/server/mod.rs index 0047fb986a..a474a1aa8c 100644 --- a/tests/server/mod.rs +++ b/tests/server/mod.rs @@ -31,7 +31,7 @@ fn run(stream: impl Read + Write, rx: &Receiver) { Message::Read(ref expected) => { let mut expected = &expected[..]; let mut expected_headers = HashSet::new(); - while let Some(i) = expected.find("\n") { + while let Some(i) = expected.find('\n') { let line = &expected[..i + 1]; expected = &expected[i + 1..]; expected_headers.insert(line); @@ -41,11 +41,11 @@ fn run(stream: impl Read + Write, rx: &Receiver) { } let mut expected_len = None; - while expected_headers.len() > 0 { + while !expected_headers.is_empty() { let mut actual = String::new(); t!(socket.read_line(&mut actual)); if actual.starts_with("Content-Length") { - let len = actual.split(": ").skip(1).next().unwrap(); + let len = actual.split(": ").nth(1).unwrap(); expected_len = len.trim().parse().ok(); } // various versions of libcurl do different things here @@ -84,13 +84,13 @@ fn run(stream: impl Read + Write, rx: &Receiver) { while socket.limit() > 0 { line.truncate(0); t!(socket.read_line(&mut line)); - if line.len() == 0 { + if line.is_empty() { break; } - if expected.len() == 0 { + if expected.is_empty() { panic!("unexpected line: {:?}", line); } - let i = expected.find("\n").unwrap_or(expected.len() - 1); + let i = expected.find('\n').unwrap_or(expected.len() - 1); let expected_line = &expected[..i + 1]; expected = &expected[i + 1..]; if lines_match(expected_line, &line) { @@ -103,7 +103,7 @@ fn run(stream: impl Read + Write, rx: &Receiver) { expected_line, line ) } - if expected.len() != 0 { + if !expected.is_empty() { println!("didn't get expected data: {:?}", expected); } }