Skip to content

Commit 7af148c

Browse files
committed
Fix or suppress warnings
1 parent cda5299 commit 7af148c

33 files changed

+202
-218
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ endif
5656
doc: build
5757
cargo doc --no-deps $(CARGO_FLAGS)
5858

59+
clippy:
60+
cargo clippy $(CARGO_FLAGS)
61+
5962
extensions: build
6063
make -C extensions/tests PY=$(PY)
6164

build.rs

+19-24
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,44 @@
11
use std::env;
2-
use std::io::Write;
32

4-
const CFG_KEY: &'static str = "py_sys_config";
3+
const CFG_KEY: &str = "py_sys_config";
54

65
#[cfg(feature = "python27-sys")]
7-
const PYTHONSYS_ENV_VAR: &'static str = "DEP_PYTHON27_PYTHON_FLAGS";
6+
const PYTHONSYS_ENV_VAR: &str = "DEP_PYTHON27_PYTHON_FLAGS";
87

98
#[cfg(feature = "python3-sys")]
10-
const PYTHONSYS_ENV_VAR: &'static str = "DEP_PYTHON3_PYTHON_FLAGS";
9+
const PYTHONSYS_ENV_VAR: &str = "DEP_PYTHON3_PYTHON_FLAGS";
1110

1211
fn main() {
13-
if cfg!(feature = "python27-sys") {
14-
if env::var_os("CARGO_FEATURE_PY_LINK_MODE_DEFAULT").is_some()
15-
|| env::var_os("CARGO_FEATURE_PY_LINK_MODE_UNRESOLVED_STATIC").is_some()
16-
{
17-
writeln!(
18-
std::io::stderr(),
19-
"Cannot use link mode control with Python 2.7"
20-
)
21-
.unwrap();
22-
std::process::exit(1);
23-
}
12+
if cfg!(feature = "python27-sys")
13+
&& (env::var_os("CARGO_FEATURE_PY_LINK_MODE_DEFAULT").is_some()
14+
|| env::var_os("CARGO_FEATURE_PY_LINK_MODE_UNRESOLVED_STATIC").is_some())
15+
{
16+
eprintln!("Cannot use link mode control with Python 2.7");
17+
std::process::exit(1);
2418
}
2519

2620
// python{27,3.x}-sys/build.rs passes python interpreter compile flags via
2721
// environment variable (using the 'links' mechanism in the cargo.toml).
2822
let flags = match env::var(PYTHONSYS_ENV_VAR) {
2923
Ok(flags) => flags,
3024
Err(_) => {
31-
writeln!(
32-
std::io::stderr(),
33-
"Environment variable {} not found - this is supposed to be \
34-
exported from the pythonXX-sys dependency, so the build chain is broken",
25+
eprintln!(
26+
concat!(
27+
"Environment variable {} not found - this is supposed to be ",
28+
"exported from the pythonXX-sys dependency, so the build ",
29+
"chain is broken"
30+
),
3531
PYTHONSYS_ENV_VAR
36-
)
37-
.unwrap();
32+
);
3833
std::process::exit(1);
3934
}
4035
};
4136

42-
if flags.len() > 0 {
43-
for f in flags.split(",") {
37+
if !flags.is_empty() {
38+
for f in flags.split(',') {
4439
// write out flags as --cfg so that the same #cfg blocks can be used
4540
// in rust-cpython as in the -sys libs
46-
let key_and_val: Vec<&str> = f.split("=").collect();
41+
let key_and_val: Vec<&str> = f.split('=').collect();
4742
let key = key_and_val[0];
4843
let val = key_and_val[1];
4944
if key.starts_with("FLAG") {

python27-sys/build.rs

+40-50
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ impl fmt::Display for PythonVersion {
2525
}
2626
}
2727

28-
const CFG_KEY: &'static str = "py_sys_config";
28+
const CFG_KEY: &str = "py_sys_config";
2929

3030
// windows' python writes out lines with the windows crlf sequence;
3131
// posix platforms and mac os should write out lines with just lf.
3232
#[cfg(target_os = "windows")]
33-
static NEWLINE_SEQUENCE: &'static str = "\r\n";
33+
static NEWLINE_SEQUENCE: &str = "\r\n";
3434

3535
#[cfg(not(target_os = "windows"))]
36-
static NEWLINE_SEQUENCE: &'static str = "\n";
36+
static NEWLINE_SEQUENCE: &str = "\n";
3737

3838
// A list of python interpreter compile-time preprocessor defines that
3939
// we will pick up and pass to rustc via --cfg=py_sys_config={varname};
@@ -48,7 +48,7 @@ static NEWLINE_SEQUENCE: &'static str = "\n";
4848
// (hrm, this is sort of re-implementing what distutils does, except
4949
// by passing command line args instead of referring to a python.h)
5050
#[cfg(not(target_os = "windows"))]
51-
static SYSCONFIG_FLAGS: [&'static str; 7] = [
51+
static SYSCONFIG_FLAGS: [&str; 7] = [
5252
"Py_USING_UNICODE",
5353
"Py_UNICODE_WIDE",
5454
"WITH_THREAD",
@@ -58,7 +58,7 @@ static SYSCONFIG_FLAGS: [&'static str; 7] = [
5858
"COUNT_ALLOCS",
5959
];
6060

61-
static SYSCONFIG_VALUES: [&'static str; 1] = [
61+
static SYSCONFIG_VALUES: [&str; 1] = [
6262
// cfg doesn't support flags with values, just bools - so flags
6363
// below are translated into bools as {varname}_{val}
6464
//
@@ -70,7 +70,7 @@ static SYSCONFIG_VALUES: [&'static str; 1] = [
7070
/// the interpreter and printing variables of interest from
7171
/// sysconfig.get_config_vars.
7272
#[cfg(not(target_os = "windows"))]
73-
fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, String> {
73+
fn get_config_vars(python_path: &str) -> Result<HashMap<String, String>, String> {
7474
let mut script = "import sysconfig; \
7575
config = sysconfig.get_config_vars();"
7676
.to_owned();
@@ -81,7 +81,7 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
8181
k,
8282
if is_value(k) { "None" } else { "0" }
8383
));
84-
script.push_str(";");
84+
script.push(';');
8585
}
8686

8787
let mut cmd = Command::new(python_path);
@@ -93,7 +93,7 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
9393

9494
if !out.status.success() {
9595
let stderr = String::from_utf8(out.stderr).unwrap();
96-
let mut msg = format!("python script failed with stderr:\n\n");
96+
let mut msg = "python script failed with stderr:\n\n".to_string();
9797
msg.push_str(&stderr);
9898
return Err(msg);
9999
}
@@ -105,15 +105,14 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
105105
"python stdout len didn't return expected number of lines:
106106
{}",
107107
split_stdout.len()
108-
)
109-
.to_string());
108+
));
110109
}
111110
let all_vars = SYSCONFIG_FLAGS.iter().chain(SYSCONFIG_VALUES.iter());
112111
// let var_map: HashMap<String, String> = HashMap::new();
113112
Ok(all_vars.zip(split_stdout.iter()).fold(
114113
HashMap::new(),
115114
|mut memo: HashMap<String, String>, (&k, &v)| {
116-
if !(v.to_owned() == "None" && is_value(k)) {
115+
if !(v == "None" && is_value(k)) {
117116
memo.insert(k.to_owned(), v.to_owned());
118117
}
119118
memo
@@ -122,7 +121,7 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
122121
}
123122

124123
#[cfg(target_os = "windows")]
125-
fn get_config_vars(_: &String) -> Result<HashMap<String, String>, String> {
124+
fn get_config_vars(_: &str) -> Result<HashMap<String, String>, String> {
126125
// sysconfig is missing all the flags on windows, so we can't actually
127126
// query the interpreter directly for its build flags.
128127
//
@@ -151,7 +150,7 @@ fn get_config_vars(_: &String) -> Result<HashMap<String, String>, String> {
151150
}
152151

153152
fn is_value(key: &str) -> bool {
154-
SYSCONFIG_VALUES.iter().find(|x| **x == key).is_some()
153+
SYSCONFIG_VALUES.iter().any(|x| *x == key)
155154
}
156155

157156
fn cfg_line_for_var(key: &str, val: &str) -> Option<String> {
@@ -185,17 +184,17 @@ fn run_python_script(interpreter: &str, script: &str) -> Result<String, String>
185184

186185
if !out.status.success() {
187186
let stderr = String::from_utf8(out.stderr).unwrap();
188-
let mut msg = format!("python script failed with stderr:\n\n");
187+
let mut msg = "python script failed with stderr:\n\n".to_string();
189188
msg.push_str(&stderr);
190189
return Err(msg);
191190
}
192191

193-
let out = String::from_utf8(out.stdout).unwrap();
194-
return Ok(out);
192+
Ok(String::from_utf8(out.stdout).unwrap())
195193
}
196194

197195
#[cfg(not(target_os = "macos"))]
198196
#[cfg(not(target_os = "windows"))]
197+
#[allow(clippy::unnecessary_wraps)]
199198
fn get_rustc_link_lib(
200199
_: &PythonVersion,
201200
ld_version: &str,
@@ -278,7 +277,7 @@ fn find_interpreter_and_get_config(
278277
let (executable, interpreter_version, lines) =
279278
get_config_from_interpreter(interpreter_path)?;
280279
if matching_version(expected_version, &interpreter_version) {
281-
return Ok((interpreter_version, executable.to_owned(), lines));
280+
return Ok((interpreter_version, executable, lines));
282281
} else {
283282
return Err(format!(
284283
"Wrong python version in PYTHON_SYS_EXECUTABLE={}\n\
@@ -297,11 +296,11 @@ fn find_interpreter_and_get_config(
297296
}
298297

299298
for name in possible_names.iter() {
300-
if let Some((executable, interpreter_version, lines)) =
301-
get_config_from_interpreter(name).ok()
299+
if let Ok((executable, interpreter_version, lines)) =
300+
get_config_from_interpreter(name)
302301
{
303302
if matching_version(expected_version, &interpreter_version) {
304-
return Ok((interpreter_version, executable.to_owned(), lines));
303+
return Ok((interpreter_version, executable, lines));
305304
}
306305
}
307306
}
@@ -371,20 +370,18 @@ fn configure_from_path(expected_version: &PythonVersion) -> Result<String, Strin
371370
println!("cargo:rustc-link-search=native={}\\libs", exec_prefix);
372371
}
373372
}
374-
} else if link_mode_unresolved_static {
375-
if cfg!(target_os = "windows") {
376-
// static-nobundle requires a Nightly rustc up to at least
377-
// Rust 1.39 (https://github.com/rust-lang/rust/issues/37403).
378-
//
379-
// We need to use static linking on Windows to prevent symbol
380-
// name mangling. Otherwise Rust will prefix extern {} symbols
381-
// with __imp_. But if we used normal "static," we need a
382-
// pythonXY.lib at build time to package into the rlib.
383-
//
384-
// static-nobundle removes the build-time library requirement,
385-
// allowing a downstream consumer to provide the pythonXY library.
386-
println!("cargo:rustc-link-lib=static-nobundle=pythonXY");
387-
}
373+
} else if link_mode_unresolved_static && cfg!(target_os = "windows") {
374+
// static-nobundle requires a Nightly rustc up to at least
375+
// Rust 1.39 (https://github.com/rust-lang/rust/issues/37403).
376+
//
377+
// We need to use static linking on Windows to prevent symbol
378+
// name mangling. Otherwise Rust will prefix extern {} symbols
379+
// with __imp_. But if we used normal "static," we need a
380+
// pythonXY.lib at build time to package into the rlib.
381+
//
382+
// static-nobundle removes the build-time library requirement,
383+
// allowing a downstream consumer to provide the pythonXY library.
384+
println!("cargo:rustc-link-lib=static-nobundle=pythonXY");
388385
}
389386

390387
if let PythonVersion {
@@ -402,7 +399,7 @@ fn configure_from_path(expected_version: &PythonVersion) -> Result<String, Strin
402399
}
403400
}
404401

405-
return Ok(interpreter_path);
402+
Ok(interpreter_path)
406403
}
407404

408405
/// Determine the python version we're supposed to be building
@@ -418,17 +415,11 @@ fn version_from_env() -> Result<PythonVersion, String> {
418415
let mut vars = env::vars().collect::<Vec<_>>();
419416
vars.sort_by(|a, b| b.cmp(a));
420417
for (key, _) in vars {
421-
match re.captures(&key) {
422-
Some(cap) => {
423-
return Ok(PythonVersion {
424-
major: cap.get(1).unwrap().as_str().parse().unwrap(),
425-
minor: match cap.get(3) {
426-
Some(s) => Some(s.as_str().parse().unwrap()),
427-
None => None,
428-
},
429-
})
430-
}
431-
None => (),
418+
if let Some(cap) = re.captures(&key) {
419+
return Ok(PythonVersion {
420+
major: cap.get(1).unwrap().as_str().parse().unwrap(),
421+
minor: cap.get(3).map(|s| s.as_str().parse().unwrap()),
422+
});
432423
}
433424
}
434425
Err(
@@ -459,9 +450,8 @@ fn main() {
459450
config_map.insert("Py_REF_DEBUG".to_owned(), "1".to_owned()); // Py_TRACE_REFS implies Py_REF_DEBUG.
460451
}
461452
for (key, val) in &config_map {
462-
match cfg_line_for_var(key, val) {
463-
Some(line) => println!("{}", line),
464-
None => (),
453+
if let Some(line) = cfg_line_for_var(key, val) {
454+
println!("{}", line);
465455
}
466456
}
467457

@@ -489,7 +479,7 @@ fn main() {
489479
});
490480
println!(
491481
"cargo:python_flags={}",
492-
if flags.len() > 0 {
482+
if !flags.is_empty() {
493483
&flags[..flags.len() - 1]
494484
} else {
495485
""

python27-sys/src/compile.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ pub struct PyFutureFeatures {
1111
pub ff_lineno: c_int,
1212
}
1313

14-
pub const FUTURE_NESTED_SCOPES: &'static str = "nested_scopes";
15-
pub const FUTURE_GENERATORS: &'static str = "generators";
16-
pub const FUTURE_DIVISION: &'static str = "division";
17-
pub const FUTURE_ABSOLUTE_IMPORT: &'static str = "absolute_import";
18-
pub const FUTURE_WITH_STATEMENT: &'static str = "with_statement";
19-
pub const FUTURE_PRINT_FUNCTION: &'static str = "print_function";
20-
pub const FUTURE_UNICODE_LITERALS: &'static str = "unicode_literals";
14+
pub const FUTURE_NESTED_SCOPES: &str = "nested_scopes";
15+
pub const FUTURE_GENERATORS: &str = "generators";
16+
pub const FUTURE_DIVISION: &str = "division";
17+
pub const FUTURE_ABSOLUTE_IMPORT: &str = "absolute_import";
18+
pub const FUTURE_WITH_STATEMENT: &str = "with_statement";
19+
pub const FUTURE_PRINT_FUNCTION: &str = "print_function";
20+
pub const FUTURE_UNICODE_LITERALS: &str = "unicode_literals";
2121

2222
#[cfg_attr(windows, link(name = "pythonXY"))]
2323
extern "C" {

python27-sys/src/fileobject.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub unsafe fn PyFile_CheckExact(op: *mut PyObject) -> c_int {
1717
(Py_TYPE(op) == &mut PyFile_Type) as c_int
1818
}
1919

20-
pub const PY_STDIOTEXTMODE: &'static str = "b";
20+
pub const PY_STDIOTEXTMODE: &str = "b";
2121

2222
#[cfg_attr(windows, link(name = "pythonXY"))]
2323
extern "C" {

python27-sys/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
non_camel_case_types,
44
non_upper_case_globals,
55
non_snake_case,
6-
unused_parens
6+
unused_parens,
7+
clippy::missing_safety_doc,
8+
clippy::transmute_ptr_to_ptr,
9+
clippy::unused_unit,
10+
clippy::identity_op,
711
)]
812

913
pub use crate::boolobject::*;

0 commit comments

Comments
 (0)