Skip to content

Commit

Permalink
Fix some Clippy warnings (#356)
Browse files Browse the repository at this point in the history
* fix collapsible if warning

* remove needlees return

* substitute write! to writeln!

* remove redundant field names in struct initialization

* add non exhaustive attribute in all enums in handler.rs

* fix inconsistent digit grouping

* simplify return of extra_description method

* fix zero pointer
  • Loading branch information
brenomfviana authored Oct 2, 2020
1 parent 87b2cbf commit badb448
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 99 deletions.
18 changes: 8 additions & 10 deletions curl-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@ fn main() {
if !cfg!(feature = "static-curl") {
// OSX and Haiku ships libcurl by default, so we just use that version
// so long as it has the right features enabled.
if target.contains("apple") || target.contains("haiku") {
if !cfg!(feature = "http2") || curl_config_reports_http2() {
return println!("cargo:rustc-flags=-l curl");
}
if (target.contains("apple") || target.contains("haiku"))
&& (!cfg!(feature = "http2") || curl_config_reports_http2())
{
return println!("cargo:rustc-flags=-l curl");
}

// Next, fall back and try to use pkg-config if its available.
if windows {
if try_vcpkg() {
return;
}
} else {
if try_pkg_config() {
return;
}
} else if try_pkg_config() {
return;
}
}

Expand Down Expand Up @@ -468,7 +466,7 @@ fn try_pkg_config() -> bool {
for path in lib.include_paths.iter() {
println!("cargo:include={}", path.display());
}
return true;
true
}

fn xcode_major_version() -> Option<u8> {
Expand Down Expand Up @@ -508,7 +506,7 @@ fn curl_config_reports_http2() -> bool {
return false;
}

return true;
true
}

fn macos_link_search_path() -> Option<String> {
Expand Down
9 changes: 5 additions & 4 deletions src/easy/form.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::ffi::CString;
use std::fmt;
use std::path::Path;
use std::ptr;

use curl_sys;
use easy::{list, List};
Expand Down Expand Up @@ -34,8 +35,8 @@ impl Form {
/// Creates a new blank form ready for the addition of new data.
pub fn new() -> Form {
Form {
head: 0 as *mut _,
tail: 0 as *mut _,
head: ptr::null_mut(),
tail: ptr::null_mut(),
headers: Vec::new(),
buffers: Vec::new(),
strings: Vec::new(),
Expand All @@ -50,10 +51,10 @@ impl Form {
Part {
error: None,
form: self,
name: name,
name,
array: vec![curl_sys::curl_forms {
option: curl_sys::CURLFORM_END,
value: 0 as *mut _,
value: ptr::null_mut(),
}],
}
}
Expand Down
82 changes: 21 additions & 61 deletions src/easy/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ffi::{CStr, CString};
use std::fmt;
use std::io::{self, SeekFrom, Write};
use std::path::Path;
use std::ptr;
use std::slice;
use std::str;
use std::time::Duration;
Expand Down Expand Up @@ -306,13 +307,12 @@ pub fn debug(kind: InfoType, data: &[u8]) {
InfoType::HeaderOut => ">",
InfoType::DataIn | InfoType::SslDataIn => "{",
InfoType::DataOut | InfoType::SslDataOut => "}",
InfoType::__Nonexhaustive => " ",
};
let mut out = out.lock();
drop(write!(out, "{} ", prefix));
match str::from_utf8(data) {
Ok(s) => drop(out.write_all(s.as_bytes())),
Err(_) => drop(write!(out, "({} bytes of data)\n", data.len())),
Err(_) => drop(writeln!(out, "({} bytes of data)", data.len())),
}
}

Expand Down Expand Up @@ -392,6 +392,7 @@ struct Inner<H> {
unsafe impl<H: Send> Send for Inner<H> {}

/// Possible proxy types that libcurl currently understands.
#[non_exhaustive]
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy)]
pub enum ProxyType {
Expand All @@ -401,43 +402,31 @@ pub enum ProxyType {
Socks5 = curl_sys::CURLPROXY_SOCKS5 as isize,
Socks4a = curl_sys::CURLPROXY_SOCKS4A as isize,
Socks5Hostname = curl_sys::CURLPROXY_SOCKS5_HOSTNAME as isize,

/// Hidden variant to indicate that this enum should not be matched on, it
/// may grow over time.
#[doc(hidden)]
__Nonexhaustive,
}

/// Possible conditions for the `time_condition` method.
#[non_exhaustive]
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy)]
pub enum TimeCondition {
None = curl_sys::CURL_TIMECOND_NONE as isize,
IfModifiedSince = curl_sys::CURL_TIMECOND_IFMODSINCE as isize,
IfUnmodifiedSince = curl_sys::CURL_TIMECOND_IFUNMODSINCE as isize,
LastModified = curl_sys::CURL_TIMECOND_LASTMOD as isize,

/// Hidden variant to indicate that this enum should not be matched on, it
/// may grow over time.
#[doc(hidden)]
__Nonexhaustive,
}

/// Possible values to pass to the `ip_resolve` method.
#[non_exhaustive]
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy)]
pub enum IpResolve {
V4 = curl_sys::CURL_IPRESOLVE_V4 as isize,
V6 = curl_sys::CURL_IPRESOLVE_V6 as isize,
Any = curl_sys::CURL_IPRESOLVE_WHATEVER as isize,

/// Hidden variant to indicate that this enum should not be matched on, it
/// may grow over time.
#[doc(hidden)]
__Nonexhaustive = 500,
}

/// Possible values to pass to the `http_version` method.
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum HttpVersion {
/// We don't care what http version to use, and we'd like the library to
Expand Down Expand Up @@ -472,14 +461,10 @@ pub enum HttpVersion {
///
/// (Added in CURL 7.66.0)
V3 = curl_sys::CURL_HTTP_VERSION_3 as isize,

/// Hidden variant to indicate that this enum should not be matched on, it
/// may grow over time.
#[doc(hidden)]
__Nonexhaustive = 500,
}

/// Possible values to pass to the `ssl_version` and `ssl_min_max_version` method.
#[non_exhaustive]
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy)]
pub enum SslVersion {
Expand All @@ -491,14 +476,10 @@ pub enum SslVersion {
Tlsv11 = curl_sys::CURL_SSLVERSION_TLSv1_1 as isize,
Tlsv12 = curl_sys::CURL_SSLVERSION_TLSv1_2 as isize,
Tlsv13 = curl_sys::CURL_SSLVERSION_TLSv1_3 as isize,

/// Hidden variant to indicate that this enum should not be matched on, it
/// may grow over time.
#[doc(hidden)]
__Nonexhaustive = 500,
}

/// Possible return values from the `seek_function` callback.
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum SeekResult {
/// Indicates that the seek operation was a success
Expand All @@ -511,15 +492,11 @@ pub enum SeekResult {
/// Indicates that although the seek failed libcurl should attempt to keep
/// working if possible (for example "seek" through reading).
CantSeek = curl_sys::CURL_SEEKFUNC_CANTSEEK as isize,

/// Hidden variant to indicate that this enum should not be matched on, it
/// may grow over time.
#[doc(hidden)]
__Nonexhaustive = 500,
}

/// Possible data chunks that can be witnessed as part of the `debug_function`
/// callback.
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum InfoType {
/// The data is informational text.
Expand All @@ -542,38 +519,25 @@ pub enum InfoType {

/// The data is SSL/TLS (binary) data sent to the peer.
SslDataOut,

/// Hidden variant to indicate that this enum should not be matched on, it
/// may grow over time.
#[doc(hidden)]
__Nonexhaustive,
}

/// Possible error codes that can be returned from the `read_function` callback.
#[non_exhaustive]
#[derive(Debug)]
pub enum ReadError {
/// Indicates that the connection should be aborted immediately
Abort,

/// Indicates that reading should be paused until `unpause` is called.
Pause,

/// Hidden variant to indicate that this enum should not be matched on, it
/// may grow over time.
#[doc(hidden)]
__Nonexhaustive,
}

/// Possible error codes that can be returned from the `write_function` callback.
#[non_exhaustive]
#[derive(Debug)]
pub enum WriteError {
/// Indicates that reading should be paused until `unpause` is called.
Pause,

/// Hidden variant to indicate that this enum should not be matched on, it
/// may grow over time.
#[doc(hidden)]
__Nonexhaustive,
}

/// Options for `.netrc` parsing.
Expand Down Expand Up @@ -623,17 +587,17 @@ impl<H: Handler> Easy2<H> {
assert!(!handle.is_null());
let mut ret = Easy2 {
inner: Box::new(Inner {
handle: handle,
handle,
header_list: None,
resolve_list: None,
connect_to_list: None,
form: None,
error_buf: RefCell::new(vec![0; curl_sys::CURL_ERROR_SIZE]),
handler: handler,
handler,
}),
};
ret.default_configure();
return ret;
ret
}
}

Expand Down Expand Up @@ -2658,7 +2622,7 @@ impl<H> Easy2<H> {
/// if the option isn't supported.
pub fn cookies(&mut self) -> Result<List, Error> {
unsafe {
let mut list = 0 as *mut _;
let mut list = ptr::null_mut();
let rc = curl_sys::curl_easy_getinfo(
self.inner.handle,
curl_sys::CURLINFO_COOKIELIST,
Expand Down Expand Up @@ -2721,7 +2685,7 @@ impl<H> Easy2<H> {
pub fn perform(&self) -> Result<(), Error> {
let ret = unsafe { self.cvt(curl_sys::curl_easy_perform(self.inner.handle)) };
panic::propagate();
return ret;
ret
}

/// Unpause reading on a connection.
Expand Down Expand Up @@ -2781,7 +2745,7 @@ impl<H> Easy2<H> {
let ret = str::from_utf8(CStr::from_ptr(p).to_bytes()).unwrap();
let ret = String::from(ret);
curl_sys::curl_free(p as *mut _);
return ret;
ret
}
}

Expand Down Expand Up @@ -2815,7 +2779,7 @@ impl<H> Easy2<H> {
let slice = slice::from_raw_parts(p as *const u8, len as usize);
let ret = slice.to_vec();
curl_sys::curl_free(p as *mut _);
return ret;
ret
}
}

Expand Down Expand Up @@ -3060,9 +3024,7 @@ extern "C" fn write_cb<H: Handler>(
let input = slice::from_raw_parts(ptr as *const u8, size * nmemb);
match (*(data as *mut Inner<H>)).handler.write(input) {
Ok(s) => s,
Err(WriteError::Pause) | Err(WriteError::__Nonexhaustive) => {
curl_sys::CURL_WRITEFUNC_PAUSE
}
Err(WriteError::Pause) => curl_sys::CURL_WRITEFUNC_PAUSE,
}
})
.unwrap_or(!0)
Expand All @@ -3079,9 +3041,7 @@ extern "C" fn read_cb<H: Handler>(
match (*(data as *mut Inner<H>)).handler.read(input) {
Ok(s) => s,
Err(ReadError::Pause) => curl_sys::CURL_READFUNC_PAUSE,
Err(ReadError::__Nonexhaustive) | Err(ReadError::Abort) => {
curl_sys::CURL_READFUNC_ABORT
}
Err(ReadError::Abort) => curl_sys::CURL_READFUNC_ABORT,
}
})
.unwrap_or(!0)
Expand Down Expand Up @@ -3145,7 +3105,7 @@ extern "C" fn debug_cb<H: Handler>(
};
(*(userptr as *mut Inner<H>)).handler.debug(kind, data)
});
return 0;
0
}

extern "C" fn ssl_ctx_cb<H: Handler>(
Expand Down
9 changes: 6 additions & 3 deletions src/easy/list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ffi::{CStr, CString};
use std::fmt;
use std::ptr;

use curl_sys;
use Error;
Expand All @@ -21,15 +22,17 @@ pub fn raw(list: &List) -> *mut curl_sys::curl_slist {
}

pub unsafe fn from_raw(raw: *mut curl_sys::curl_slist) -> List {
List { raw: raw }
List { raw }
}

unsafe impl Send for List {}

impl List {
/// Creates a new empty list of strings.
pub fn new() -> List {
List { raw: 0 as *mut _ }
List {
raw: ptr::null_mut(),
}
}

/// Appends some data into this list.
Expand Down Expand Up @@ -86,7 +89,7 @@ impl<'a> Iterator for Iter<'a> {
unsafe {
let ret = Some(CStr::from_ptr((*self.cur).data).to_bytes());
self.cur = (*self.cur).next;
return ret;
ret
}
}
}
Expand Down
Loading

0 comments on commit badb448

Please sign in to comment.