Skip to content

Commit 08e34dc

Browse files
committed
chore: lint ctest crate
run `cargo clippy --all-targets` on `ctest`, and fix all default issues.
1 parent 0fe1d45 commit 08e34dc

File tree

2 files changed

+32
-45
lines changed

2 files changed

+32
-45
lines changed

ctest/Cargo.toml

+1-13
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ indoc = "2.0.6"
1818
# once MSRV is above 1.64 and replaced with `[lints] workspace=true`
1919

2020
[lints.rust]
21-
# FIXME(cleanup): make ident usage consistent in each file
22-
unused_qualifications = "allow"
21+
unused_qualifications = "warn"
2322

2423
[lints.clippy]
25-
# FIXME(clippy): fix these
26-
doc_lazy_continuation = "allow"
27-
if_same_then_else = "allow"
28-
needless_borrow = "allow"
29-
needless_borrowed_reference = "allow"
30-
needless_borrows_for_generic_args = "allow"
31-
needless_lifetimes = "allow"
32-
only_used_in_recursion = "allow"
33-
option_as_ref_deref = "allow"
34-
type_complexity = "allow"
35-
useless_format = "allow"

ctest/src/lib.rs

+31-32
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub enum VolatileItemKind {
7474
/// This builder has a number of configuration options which modify how the
7575
/// generated tests are emitted, and it is also the main entry point for parsing
7676
/// an FFI header crate for definitions.
77+
#[allow(clippy::type_complexity)]
7778
pub struct TestGenerator {
7879
headers: Vec<String>,
7980
includes: Vec<PathBuf>,
@@ -163,7 +164,7 @@ impl TestGenerator {
163164
f.to_string()
164165
}
165166
}),
166-
const_cname: Box::new(std::string::ToString::to_string),
167+
const_cname: Box::new(ToString::to_string),
167168
rust_version: rustc_version::version().unwrap(),
168169
}
169170
}
@@ -324,7 +325,7 @@ impl TestGenerator {
324325
/// ```
325326
pub fn define(&mut self, k: &str, v: Option<&str>) -> &mut Self {
326327
self.defines
327-
.push((k.to_string(), v.map(std::string::ToString::to_string)));
328+
.push((k.to_string(), v.map(ToString::to_string)));
328329
self
329330
}
330331

@@ -337,10 +338,10 @@ impl TestGenerator {
337338
/// optional value of `v`:
338339
///
339340
/// * `k == "foo"` and `v == None` makes `#[cfg(foo)]` expand. That is,
340-
/// `cfg!(foo)` expands to `true`.
341+
/// `cfg!(foo)` expands to `true`.
341342
///
342343
/// * `k == "bar"` and `v == Some("baz")` makes `#[cfg(bar = "baz")]`
343-
/// expand. That is, `cfg!(bar = "baz")` expands to `true`.
344+
/// expand. That is, `cfg!(bar = "baz")` expands to `true`.
344345
///
345346
/// # Examples
346347
///
@@ -353,7 +354,7 @@ impl TestGenerator {
353354
/// ```
354355
pub fn cfg(&mut self, k: &str, v: Option<&str>) -> &mut Self {
355356
self.cfg
356-
.push((k.to_string(), v.map(std::string::ToString::to_string)));
357+
.push((k.to_string(), v.map(ToString::to_string)));
357358
self
358359
}
359360

@@ -811,7 +812,7 @@ impl TestGenerator {
811812
Lang::C => "c",
812813
Lang::CXX => "cpp",
813814
};
814-
cfg.file(&out.with_extension(ext));
815+
cfg.file(out.with_extension(ext));
815816
if target.contains("msvc") {
816817
cfg.flag("/W3").flag("/Wall").flag("/WX")
817818
// ignored warnings
@@ -844,7 +845,7 @@ impl TestGenerator {
844845
cfg.flag(flag);
845846
}
846847

847-
for &(ref a, ref b) in &self.defines {
848+
for (a, b) in &self.defines {
848849
cfg.define(a, b.as_ref().map(|s| &s[..]));
849850
}
850851
for p in &self.includes {
@@ -884,7 +885,7 @@ impl TestGenerator {
884885
.clone()
885886
.unwrap_or_else(|| env::var("TARGET").unwrap());
886887
for (k, v) in default_cfg(&target).into_iter().chain(self.cfg.clone()) {
887-
let s = |s: &str| ast::Name::intern(s);
888+
let s = |s: &str| Name::intern(s);
888889
sess.config.insert((s(&k), v.as_ref().map(|n| s(n))));
889890
}
890891

@@ -995,9 +996,7 @@ fn default_cfg(target: &str) -> Vec<(String, Option<String>)> {
995996
("powerpc", "32", "big")
996997
} else if target.starts_with("s390x") {
997998
("s390x", "64", "big")
998-
} else if target.starts_with("sparc64") {
999-
("sparc64", "64", "big")
1000-
} else if target.starts_with("sparcv9") {
999+
} else if target.starts_with("sparc64") || target.starts_with("sparcv9") {
10011000
("sparc64", "64", "big")
10021001
} else if target.starts_with("asmjs") {
10031002
("asmjs", "32", "little")
@@ -1092,7 +1091,7 @@ fn linkage(lang: &Lang) -> &'static str {
10921091
}
10931092
}
10941093

1095-
impl<'a> Generator<'a> {
1094+
impl Generator<'_> {
10961095
fn rust2c_test(&self, ty: &str) -> bool {
10971096
let rustc_types = [
10981097
"usize", "u8", "u16", "u32", "u64", "isize", "i8", "i16", "i32", "i64",
@@ -1116,7 +1115,7 @@ impl<'a> Generator<'a> {
11161115

11171116
fn rust2c(&self, ty: &str) -> String {
11181117
match ty {
1119-
"c_longdouble" | "c_long_double" => format!("long double"),
1118+
"c_longdouble" | "c_long_double" => "long double".to_string(),
11201119
t if t.starts_with("c_") => match &ty[2..].replace("long", " long")[..] {
11211120
s if s.starts_with('u') => format!("unsigned {}", &s[1..]),
11221121
"short" => "short".to_string(),
@@ -1984,8 +1983,8 @@ impl<'a> Generator<'a> {
19841983
ast::TyKind::Path(_, ref path) => {
19851984
let last = path.segments.last().unwrap();
19861985
if last.identifier.to_string() == "Option" {
1987-
match last.parameters.as_ref().map(|p| &**p) {
1988-
Some(&ast::PathParameters::AngleBracketed(ref p)) => {
1986+
match last.parameters.as_deref() {
1987+
Some(ast::PathParameters::AngleBracketed(p)) => {
19891988
self.ty2name(&p.types[0], rust)
19901989
}
19911990
_ => panic!(),
@@ -2017,7 +2016,7 @@ impl<'a> Generator<'a> {
20172016
format!("{} {}*", self.ty2name(&t.ty, rust), modifier)
20182017
}
20192018
ast::TyKind::Array(ref t, ref e) => {
2020-
let len = self.expr2str(e);
2019+
let len = Self::expr2str(e);
20212020
let ty = self.ty2name(t, rust);
20222021
format!("{} {} [{}]", modifier, ty, len)
20232022
}
@@ -2056,9 +2055,9 @@ impl<'a> Generator<'a> {
20562055
}
20572056
ast::TyKind::Array(ref t, ref e) => {
20582057
if rust {
2059-
format!("[{}; {}]", self.ty2name(t, rust), self.expr2str(e))
2058+
format!("[{}; {}]", self.ty2name(t, rust), Self::expr2str(e))
20602059
} else {
2061-
let len = self.expr2str(e);
2060+
let len = Self::expr2str(e);
20622061
let ty = self.ty2name(t, rust);
20632062
format!("{} [{}]", ty, len)
20642063
}
@@ -2125,8 +2124,8 @@ impl<'a> Generator<'a> {
21252124
if path.segments.last().unwrap().identifier.to_string() == "Option" =>
21262125
{
21272126
let last = path.segments.last().unwrap();
2128-
match last.parameters.as_ref().map(|s| &**s) {
2129-
Some(&ast::PathParameters::AngleBracketed(ref p)) => {
2127+
match last.parameters.as_deref() {
2128+
Some(ast::PathParameters::AngleBracketed(p)) => {
21302129
self.csig_returning_ptr(&p.types[0], sig)
21312130
}
21322131
_ => panic!(),
@@ -2148,16 +2147,16 @@ impl<'a> Generator<'a> {
21482147
"{}(*{})[{}][{}]",
21492148
self.ty2name(t2, false),
21502149
sig,
2151-
self.expr2str(e),
2152-
self.expr2str(e2)
2150+
Self::expr2str(e),
2151+
Self::expr2str(e2)
21532152
),
2154-
_ => format!("{}(*{})[{}]", self.ty2name(t, false), sig, self.expr2str(e)),
2153+
_ => format!("{}(*{})[{}]", self.ty2name(t, false), sig, Self::expr2str(e)),
21552154
},
21562155
_ => format!("{}* {}", self.ty2name(ty, false), sig),
21572156
}
21582157
}
21592158

2160-
fn expr2str(&self, e: &ast::Expr) -> String {
2159+
fn expr2str(e: &ast::Expr) -> String {
21612160
match e.node {
21622161
ast::ExprKind::Lit(ref l) => match l.node {
21632162
ast::LitKind::Int(a, _) => a.to_string(),
@@ -2166,10 +2165,10 @@ impl<'a> Generator<'a> {
21662165
ast::ExprKind::Path(_, ref path) => {
21672166
path.segments.last().unwrap().identifier.to_string()
21682167
}
2169-
ast::ExprKind::Cast(ref e, _) => self.expr2str(e),
2168+
ast::ExprKind::Cast(ref e, _) => Self::expr2str(e),
21702169
ast::ExprKind::Binary(ref op, ref e1, ref e2) => {
2171-
let e1 = self.expr2str(e1);
2172-
let e2 = self.expr2str(e2);
2170+
let e1 = Self::expr2str(e1);
2171+
let e2 = Self::expr2str(e2);
21732172
match op.node {
21742173
ast::BinOpKind::Add => format!("{} + {}", e1, e2),
21752174
ast::BinOpKind::Sub => format!("{} - {}", e1, e2),
@@ -2248,7 +2247,7 @@ impl<'a> Generator<'a> {
22482247
}
22492248
}
22502249

2251-
impl<'a, 'v> Visitor<'v> for Generator<'a> {
2250+
impl<'v> Visitor<'v> for Generator<'_> {
22522251
fn visit_item(&mut self, i: &'v ast::Item) {
22532252
let prev_abi = self.abi;
22542253
let public = i.vis == ast::Visibility::Public;
@@ -2316,8 +2315,8 @@ impl<'a, 'v> Visitor<'v> for Generator<'a> {
23162315
.unwrap();
23172316
}
23182317
ast::ForeignItemKind::Static(ref ty, mutbl) => {
2319-
let rust_ty = self.ty2name(&ty, true);
2320-
let c_ty = self.ty2name(&ty, false);
2318+
let rust_ty = self.ty2name(ty, true);
2319+
let c_ty = self.ty2name(ty, false);
23212320
let c_name = attr::first_attr_value_str_by_name(&i.attrs, "link_name")
23222321
.map(|i| i.to_string());
23232322
self.test_extern_static(&i.ident.to_string(), c_name, &rust_ty, &c_ty, mutbl)
@@ -2356,7 +2355,7 @@ struct MyResolver<'a> {
23562355
map: HashMap<Name, Rc<SyntaxExtension>>,
23572356
}
23582357

2359-
impl<'a> Resolver for MyResolver<'a> {
2358+
impl Resolver for MyResolver<'_> {
23602359
fn next_node_id(&mut self) -> ast::NodeId {
23612360
self.id += 1;
23622361
ast::NodeId::new(self.id)
@@ -2463,7 +2462,7 @@ struct MyVisitor<'b> {
24632462
map: &'b mut HashMap<Name, Rc<SyntaxExtension>>,
24642463
}
24652464

2466-
impl<'a, 'b> Visitor<'a> for MyVisitor<'b> {
2465+
impl<'a> Visitor<'a> for MyVisitor<'_> {
24672466
fn visit_item(&mut self, item: &'a ast::Item) {
24682467
if let ast::ItemKind::MacroDef(..) = item.node {
24692468
self.map.insert(

0 commit comments

Comments
 (0)