Skip to content

Commit 4f3050b

Browse files
authored
Rollup merge of rust-lang#121543 - onur-ozkan:clippy-args, r=oli-obk
various clippy fixes We need to keep the order of the given clippy lint rules before passing them. Since clap doesn't offer any useful interface for this purpose out of the box, we have to handle it manually. Additionally, this PR makes `-D` rules work as expected. Previously, lint rules were limited to `-W`. By enabling `-D`, clippy began to complain numerous lines in the tree, all of which have been resolved in this PR as well. Fixes rust-lang#121481 cc `@matthiaskrgr`
2 parents b7dcabe + 81d7d7a commit 4f3050b

File tree

11 files changed

+107
-37
lines changed

11 files changed

+107
-37
lines changed

compiler/rustc_ast/src/token.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1111
use rustc_data_structures::sync::Lrc;
1212
use rustc_macros::HashStable_Generic;
1313
use rustc_span::symbol::{kw, sym};
14+
#[allow(clippy::useless_attribute)] // FIXME: following use of `hidden_glob_reexports` incorrectly triggers `useless_attribute` lint.
1415
#[allow(hidden_glob_reexports)]
1516
use rustc_span::symbol::{Ident, Symbol};
1617
use rustc_span::{edition::Edition, ErrorGuaranteed, Span, DUMMY_SP};

compiler/rustc_codegen_llvm/src/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ pub unsafe fn create_module<'ll>(
315315
//
316316
// On the wasm targets it will get hooked up to the "producer" sections
317317
// `processed-by` information.
318+
#[allow(clippy::option_env_unwrap)]
318319
let rustc_producer =
319320
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
320321
let name_metadata = llvm::LLVMMDStringInContext(

compiler/rustc_const_eval/src/interpret/intern.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ pub fn intern_const_alloc_for_constprop<
293293
return Ok(());
294294
}
295295
// Move allocation to `tcx`.
296-
for _ in intern_shallow(ecx, alloc_id, Mutability::Not).map_err(|()| err_ub!(DeadLocal))? {
296+
if let Some(_) =
297+
(intern_shallow(ecx, alloc_id, Mutability::Not).map_err(|()| err_ub!(DeadLocal))?).next()
298+
{
297299
// We are not doing recursive interning, so we don't currently support provenance.
298300
// (If this assertion ever triggers, we should just implement a
299301
// proper recursive interning loop -- or just call `intern_const_alloc_recursive`.

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,7 @@ impl<D: Decoder> Decodable<D> for EncodedMetadata {
21982198
let mmap = if len > 0 {
21992199
let mut mmap = MmapMut::map_anon(len).unwrap();
22002200
for _ in 0..len {
2201-
(&mut mmap[..]).write(&[d.read_u8()]).unwrap();
2201+
(&mut mmap[..]).write_all(&[d.read_u8()]).unwrap();
22022202
}
22032203
mmap.flush().unwrap();
22042204
Some(mmap.make_read_only().unwrap())

compiler/rustc_middle/src/hir/map/mod.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,16 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> {
7676
if self.current_id == CRATE_HIR_ID {
7777
return None;
7878
}
79-
loop {
80-
// There are nodes that do not have entries, so we need to skip them.
81-
let parent_id = self.map.def_key(self.current_id.owner.def_id).parent;
8279

83-
let parent_id = parent_id.map_or(CRATE_OWNER_ID, |local_def_index| {
84-
let def_id = LocalDefId { local_def_index };
85-
self.map.tcx.local_def_id_to_hir_id(def_id).owner
86-
});
87-
self.current_id = HirId::make_owner(parent_id.def_id);
80+
let parent_id = self.map.def_key(self.current_id.owner.def_id).parent;
81+
let parent_id = parent_id.map_or(CRATE_OWNER_ID, |local_def_index| {
82+
let def_id = LocalDefId { local_def_index };
83+
self.map.tcx.local_def_id_to_hir_id(def_id).owner
84+
});
85+
self.current_id = HirId::make_owner(parent_id.def_id);
8886

89-
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
90-
let node = self.map.tcx.hir_owner_node(self.current_id.owner);
91-
return Some((self.current_id.owner, node));
92-
}
87+
let node = self.map.tcx.hir_owner_node(self.current_id.owner);
88+
return Some((self.current_id.owner, node));
9389
}
9490
}
9591

compiler/rustc_middle/src/mir/interpret/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,11 @@ pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, i
671671
// So we do not read exactly 16 bytes into the u128, just the "payload".
672672
let uint = match endianness {
673673
Endian::Little => {
674-
source.read(&mut buf)?;
674+
source.read_exact(&mut buf[..source.len()])?;
675675
Ok(u128::from_le_bytes(buf))
676676
}
677677
Endian::Big => {
678-
source.read(&mut buf[16 - source.len()..])?;
678+
source.read_exact(&mut buf[16 - source.len()..])?;
679679
Ok(u128::from_be_bytes(buf))
680680
}
681681
};

compiler/rustc_mir_build/src/build/matches/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
229229
span: Span,
230230
scrutinee_span: Span,
231231
) -> BlockAnd<()> {
232-
let scrutinee_span = scrutinee_span;
233232
let scrutinee_place =
234233
unpack!(block = self.lower_scrutinee(block, scrutinee_id, scrutinee_span));
235234

compiler/stable_mir/src/mir/alloc.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ pub(crate) fn read_target_uint(mut bytes: &[u8]) -> Result<u128, Error> {
5757
let mut buf = [0u8; std::mem::size_of::<u128>()];
5858
match MachineInfo::target_endianess() {
5959
Endian::Little => {
60-
bytes.read(&mut buf)?;
60+
bytes.read_exact(&mut buf[..bytes.len()])?;
6161
Ok(u128::from_le_bytes(buf))
6262
}
6363
Endian::Big => {
64-
bytes.read(&mut buf[16 - bytes.len()..])?;
64+
bytes.read_exact(&mut buf[16 - bytes.len()..])?;
6565
Ok(u128::from_be_bytes(buf))
6666
}
6767
}
@@ -72,11 +72,11 @@ pub(crate) fn read_target_int(mut bytes: &[u8]) -> Result<i128, Error> {
7272
let mut buf = [0u8; std::mem::size_of::<i128>()];
7373
match MachineInfo::target_endianess() {
7474
Endian::Little => {
75-
bytes.read(&mut buf)?;
75+
bytes.read_exact(&mut buf[..bytes.len()])?;
7676
Ok(i128::from_le_bytes(buf))
7777
}
7878
Endian::Big => {
79-
bytes.read(&mut buf[16 - bytes.len()..])?;
79+
bytes.read_exact(&mut buf[16 - bytes.len()..])?;
8080
Ok(i128::from_be_bytes(buf))
8181
}
8282
}

library/std/src/io/buffered/bufreader.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,9 @@ impl<R: ?Sized + Read> Read for BufReader<R> {
328328
self.discard_buffer();
329329
return self.inner.read_vectored(bufs);
330330
}
331-
let nread = {
332-
let mut rem = self.fill_buf()?;
333-
rem.read_vectored(bufs)?
334-
};
331+
let mut rem = self.fill_buf()?;
332+
let nread = rem.read_vectored(bufs)?;
333+
335334
self.consume(nread);
336335
Ok(nread)
337336
}

src/bootstrap/src/core/build_steps/check.rs

+35-7
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,49 @@ fn args(builder: &Builder<'_>) -> Vec<String> {
6161
}
6262
}
6363

64-
args.extend(strings(&["--", "--cap-lints", "warn"]));
64+
args.extend(strings(&["--"]));
65+
66+
if deny.is_empty() && forbid.is_empty() {
67+
args.extend(strings(&["--cap-lints", "warn"]));
68+
}
69+
70+
let all_args = std::env::args().collect::<Vec<_>>();
71+
args.extend(get_clippy_rules_in_order(&all_args, allow, deny, warn, forbid));
72+
6573
args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint)));
66-
let mut clippy_lint_levels: Vec<String> = Vec::new();
67-
allow.iter().for_each(|v| clippy_lint_levels.push(format!("-A{}", v)));
68-
deny.iter().for_each(|v| clippy_lint_levels.push(format!("-D{}", v)));
69-
warn.iter().for_each(|v| clippy_lint_levels.push(format!("-W{}", v)));
70-
forbid.iter().for_each(|v| clippy_lint_levels.push(format!("-F{}", v)));
71-
args.extend(clippy_lint_levels);
7274
args.extend(builder.config.free_args.clone());
7375
args
7476
} else {
7577
builder.config.free_args.clone()
7678
}
7779
}
7880

81+
/// We need to keep the order of the given clippy lint rules before passing them.
82+
/// Since clap doesn't offer any useful interface for this purpose out of the box,
83+
/// we have to handle it manually.
84+
pub(crate) fn get_clippy_rules_in_order(
85+
all_args: &[String],
86+
allow_rules: &[String],
87+
deny_rules: &[String],
88+
warn_rules: &[String],
89+
forbid_rules: &[String],
90+
) -> Vec<String> {
91+
let mut result = vec![];
92+
93+
for (prefix, item) in
94+
[("-A", allow_rules), ("-D", deny_rules), ("-W", warn_rules), ("-F", forbid_rules)]
95+
{
96+
item.iter().for_each(|v| {
97+
let rule = format!("{prefix}{v}");
98+
let position = all_args.iter().position(|t| t == &rule).unwrap();
99+
result.push((position, rule));
100+
});
101+
}
102+
103+
result.sort_by_key(|&(position, _)| position);
104+
result.into_iter().map(|v| v.1).collect()
105+
}
106+
79107
fn cargo_subcommand(kind: Kind) -> &'static str {
80108
match kind {
81109
Kind::Check => "check",

src/bootstrap/src/core/config/tests.rs

+49-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{flags::Flags, ChangeIdWrapper, Config};
2+
use crate::core::build_steps::check::get_clippy_rules_in_order;
23
use crate::core::config::{LldMode, TomlConfig};
34

45
use clap::CommandFactory;
@@ -11,12 +12,13 @@ use std::{
1112
};
1213

1314
fn parse(config: &str) -> Config {
14-
let config = format!("{config} \r\n build.rustc = \"/does-not-exists\" ");
1515
Config::parse_inner(
1616
&[
17-
"check".to_owned(),
18-
"--config=/does/not/exist".to_owned(),
19-
"--skip-stage0-validation".to_owned(),
17+
"check".to_string(),
18+
"--set=build.rustc=/does/not/exist".to_string(),
19+
"--set=build.cargo=/does/not/exist".to_string(),
20+
"--config=/does/not/exist".to_string(),
21+
"--skip-stage0-validation".to_string(),
2022
],
2123
|&_| toml::from_str(&config).unwrap(),
2224
)
@@ -169,7 +171,10 @@ fn override_toml_duplicate() {
169171
Config::parse_inner(
170172
&[
171173
"check".to_owned(),
174+
"--set=build.rustc=/does/not/exist".to_string(),
175+
"--set=build.cargo=/does/not/exist".to_string(),
172176
"--config=/does/not/exist".to_owned(),
177+
"--skip-stage0-validation".to_owned(),
173178
"--set=change-id=1".to_owned(),
174179
"--set=change-id=2".to_owned(),
175180
],
@@ -192,7 +197,15 @@ fn profile_user_dist() {
192197
.and_then(|table: toml::Value| TomlConfig::deserialize(table))
193198
.unwrap()
194199
}
195-
Config::parse_inner(&["check".to_owned()], get_toml);
200+
Config::parse_inner(
201+
&[
202+
"check".to_owned(),
203+
"--set=build.rustc=/does/not/exist".to_string(),
204+
"--set=build.cargo=/does/not/exist".to_string(),
205+
"--skip-stage0-validation".to_string(),
206+
],
207+
get_toml,
208+
);
196209
}
197210

198211
#[test]
@@ -254,3 +267,34 @@ fn parse_change_id_with_unknown_field() {
254267
let change_id_wrapper: ChangeIdWrapper = toml::from_str(config).unwrap();
255268
assert_eq!(change_id_wrapper.inner, Some(3461));
256269
}
270+
271+
#[test]
272+
fn order_of_clippy_rules() {
273+
let args = vec![
274+
"clippy".to_string(),
275+
"--fix".to_string(),
276+
"--allow-dirty".to_string(),
277+
"--allow-staged".to_string(),
278+
"-Aclippy:all".to_string(),
279+
"-Wclippy::style".to_string(),
280+
"-Aclippy::foo1".to_string(),
281+
"-Aclippy::foo2".to_string(),
282+
];
283+
let config = Config::parse(&args);
284+
285+
let actual = match &config.cmd {
286+
crate::Subcommand::Clippy { allow, deny, warn, forbid, .. } => {
287+
get_clippy_rules_in_order(&args, &allow, &deny, &warn, &forbid)
288+
}
289+
_ => panic!("invalid subcommand"),
290+
};
291+
292+
let expected = vec![
293+
"-Aclippy:all".to_string(),
294+
"-Wclippy::style".to_string(),
295+
"-Aclippy::foo1".to_string(),
296+
"-Aclippy::foo2".to_string(),
297+
];
298+
299+
assert_eq!(expected, actual);
300+
}

0 commit comments

Comments
 (0)