Skip to content

Commit e0947d4

Browse files
committed
Auto merge of rust-lang#119552 - krtab:dead_code_priv_mod_pub_field, r=cjgillot
Replace visibility test with reachability test in dead code detection Fixes rust-lang#119545 Also included is a fix for an error now flagged by the lint
2 parents 25f8d01 + 1e68460 commit e0947d4

File tree

26 files changed

+121
-41
lines changed

26 files changed

+121
-41
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2098,7 +2098,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20982098
from_closure: constraint.from_closure,
20992099
cause: ObligationCause::new(constraint.span, CRATE_DEF_ID, cause_code.clone()),
21002100
variance_info: constraint.variance_info,
2101-
outlives_constraint: *constraint,
21022101
})
21032102
.collect();
21042103
debug!("categorized_path={:#?}", categorized_path);
@@ -2327,5 +2326,4 @@ pub struct BlameConstraint<'tcx> {
23272326
pub from_closure: bool,
23282327
pub cause: ObligationCause<'tcx>,
23292328
pub variance_info: ty::VarianceDiagInfo<'tcx>,
2330-
pub outlives_constraint: OutlivesConstraint<'tcx>,
23312329
}

compiler/rustc_builtin_macros/src/deriving/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
5252

5353
let (ident, vdata, fields) = match substr.fields {
5454
Struct(vdata, fields) => (substr.type_ident, *vdata, fields),
55-
EnumMatching(_, _, v, fields) => (v.ident, &v.data, fields),
55+
EnumMatching(_, v, fields) => (v.ident, &v.data, fields),
5656
AllFieldlessEnum(enum_def) => return show_fieldless_enum(cx, span, enum_def, substr),
5757
EnumTag(..) | StaticStruct(..) | StaticEnum(..) => {
5858
cx.dcx().span_bug(span, "nonsensical .fields in `#[derive(Debug)]`")

compiler/rustc_builtin_macros/src/deriving/encodable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ fn encodable_substructure(
226226
BlockOrExpr::new_expr(expr)
227227
}
228228

229-
EnumMatching(idx, _, variant, fields) => {
229+
EnumMatching(idx, variant, fields) => {
230230
// We're not generating an AST that the borrow checker is expecting,
231231
// so we need to generate a unique local variable to take the
232232
// mutable loan out on, otherwise we get conflicts which don't

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ pub enum SubstructureFields<'a> {
304304
/// variants has any fields).
305305
AllFieldlessEnum(&'a ast::EnumDef),
306306

307-
/// Matching variants of the enum: variant index, variant count, ast::Variant,
307+
/// Matching variants of the enum: variant index, ast::Variant,
308308
/// fields: the field name is only non-`None` in the case of a struct
309309
/// variant.
310-
EnumMatching(usize, usize, &'a ast::Variant, Vec<FieldInfo>),
310+
EnumMatching(usize, &'a ast::Variant, Vec<FieldInfo>),
311311

312312
/// The tag of an enum. The first field is a `FieldInfo` for the tags, as
313313
/// if they were fields. The second field is the expression to combine the
@@ -1270,7 +1270,7 @@ impl<'a> MethodDef<'a> {
12701270
trait_,
12711271
type_ident,
12721272
nonselflike_args,
1273-
&EnumMatching(0, 1, &variants[0], Vec::new()),
1273+
&EnumMatching(0, &variants[0], Vec::new()),
12741274
);
12751275
}
12761276
}
@@ -1316,7 +1316,7 @@ impl<'a> MethodDef<'a> {
13161316
// expressions for referencing every field of every
13171317
// Self arg, assuming all are instances of VariantK.
13181318
// Build up code associated with such a case.
1319-
let substructure = EnumMatching(index, variants.len(), variant, fields);
1319+
let substructure = EnumMatching(index, variant, fields);
13201320
let arm_expr = self
13211321
.call_substructure_method(
13221322
cx,
@@ -1344,7 +1344,7 @@ impl<'a> MethodDef<'a> {
13441344
trait_,
13451345
type_ident,
13461346
nonselflike_args,
1347-
&EnumMatching(0, variants.len(), v, Vec::new()),
1347+
&EnumMatching(0, v, Vec::new()),
13481348
)
13491349
.into_expr(cx, span),
13501350
)

compiler/rustc_codegen_gcc/build_system/src/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::env as std_env;
55
pub struct ConfigInfo {
66
pub target: String,
77
pub target_triple: String,
8+
#[allow(dead_code)]
89
pub rustc_command: Vec<String>,
910
}
1011

compiler/rustc_codegen_gcc/src/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pub struct CodegenCx<'gcc, 'tcx> {
106106
local_gen_sym_counter: Cell<usize>,
107107

108108
eh_personality: Cell<Option<RValue<'gcc>>>,
109+
#[cfg(feature="master")]
109110
pub rust_try_fn: Cell<Option<(Type<'gcc>, Function<'gcc>)>>,
110111

111112
pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
@@ -117,6 +118,7 @@ pub struct CodegenCx<'gcc, 'tcx> {
117118
/// FIXME(antoyo): fix the rustc API to avoid having this hack.
118119
pub structs_as_pointer: RefCell<FxHashSet<RValue<'gcc>>>,
119120

121+
#[cfg(feature="master")]
120122
pub cleanup_blocks: RefCell<FxHashSet<Block<'gcc>>>,
121123
}
122124

@@ -269,9 +271,11 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
269271
struct_types: Default::default(),
270272
local_gen_sym_counter: Cell::new(0),
271273
eh_personality: Cell::new(None),
274+
#[cfg(feature="master")]
272275
rust_try_fn: Cell::new(None),
273276
pointee_infos: Default::default(),
274277
structs_as_pointer: Default::default(),
278+
#[cfg(feature="master")]
275279
cleanup_blocks: Default::default(),
276280
};
277281
// TODO(antoyo): instead of doing this, add SsizeT to libgccjit.

compiler/rustc_codegen_llvm/src/context.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ use rustc_session::config::{CrateType, DebugInfo, PAuthKey, PacRet};
2727
use rustc_session::Session;
2828
use rustc_span::source_map::Spanned;
2929
use rustc_span::Span;
30-
use rustc_target::abi::{
31-
call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx,
32-
};
30+
use rustc_target::abi::{call::FnAbi, HasDataLayout, TargetDataLayout, VariantIdx};
3331
use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
3432
use smallvec::SmallVec;
3533

@@ -82,7 +80,6 @@ pub struct CodegenCx<'ll, 'tcx> {
8280
/// Mapping of scalar types to llvm types.
8381
pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
8482

85-
pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
8683
pub isize_ty: &'ll Type,
8784

8885
pub coverage_cx: Option<coverageinfo::CrateCoverageContext<'ll, 'tcx>>,
@@ -487,7 +484,6 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
487484
compiler_used_statics: RefCell::new(Vec::new()),
488485
type_lowering: Default::default(),
489486
scalar_lltypes: Default::default(),
490-
pointee_infos: Default::default(),
491487
isize_ty,
492488
coverage_cx,
493489
dbg_cx,

compiler/rustc_mir_transform/src/gvn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
132132
|local, value, location| {
133133
let value = match value {
134134
// We do not know anything of this assigned value.
135-
AssignedValue::Arg | AssignedValue::Terminator(_) => None,
135+
AssignedValue::Arg | AssignedValue::Terminator => None,
136136
// Try to get some insight.
137137
AssignedValue::Rvalue(rvalue) => {
138138
let value = state.simplify_rvalue(rvalue, location);

compiler/rustc_mir_transform/src/ssa.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct SsaLocals {
2929
pub enum AssignedValue<'a, 'tcx> {
3030
Arg,
3131
Rvalue(&'a mut Rvalue<'tcx>),
32-
Terminator(&'a mut TerminatorKind<'tcx>),
32+
Terminator,
3333
}
3434

3535
impl SsaLocals {
@@ -149,8 +149,7 @@ impl SsaLocals {
149149
Set1::One(DefLocation::CallReturn { call, .. }) => {
150150
let bb = &mut basic_blocks[call];
151151
let loc = Location { block: call, statement_index: bb.statements.len() };
152-
let term = bb.terminator_mut();
153-
f(local, AssignedValue::Terminator(&mut term.kind), loc)
152+
f(local, AssignedValue::Terminator, loc)
154153
}
155154
_ => {}
156155
}

compiler/rustc_passes/src/dead.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -437,15 +437,16 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
437437
let tcx = self.tcx;
438438
let has_repr_c = self.repr_has_repr_c;
439439
let has_repr_simd = self.repr_has_repr_simd;
440+
let effective_visibilities = &tcx.effective_visibilities(());
440441
let live_fields = def.fields().iter().filter_map(|f| {
441442
let def_id = f.def_id;
442443
if has_repr_c || (f.is_positional() && has_repr_simd) {
443444
return Some(def_id);
444445
}
445-
if !tcx.visibility(f.hir_id.owner.def_id).is_public() {
446+
if !effective_visibilities.is_reachable(f.hir_id.owner.def_id) {
446447
return None;
447448
}
448-
if tcx.visibility(def_id).is_public() { Some(def_id) } else { None }
449+
if effective_visibilities.is_reachable(def_id) { Some(def_id) } else { None }
449450
});
450451
self.live_symbols.extend(live_fields);
451452

library/alloc/src/collections/btree/navigate.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
655655
pub enum Position<BorrowType, K, V> {
656656
Leaf(NodeRef<BorrowType, K, V, marker::Leaf>),
657657
Internal(NodeRef<BorrowType, K, V, marker::Internal>),
658-
InternalKV(Handle<NodeRef<BorrowType, K, V, marker::Internal>, marker::KV>),
658+
InternalKV,
659659
}
660660

661661
impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal> {
@@ -677,7 +677,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>
677677
visit(Position::Leaf(leaf));
678678
match edge.next_kv() {
679679
Ok(kv) => {
680-
visit(Position::InternalKV(kv));
680+
visit(Position::InternalKV);
681681
kv.right_edge()
682682
}
683683
Err(_) => return,
@@ -699,7 +699,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>
699699
self.visit_nodes_in_order(|pos| match pos {
700700
Position::Leaf(node) => result += node.len(),
701701
Position::Internal(node) => result += node.len(),
702-
Position::InternalKV(_) => (),
702+
Position::InternalKV => (),
703703
});
704704
result
705705
}

library/alloc/src/collections/btree/node/tests.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>
3232
result += &format!("\n{}{:?}", indent, leaf.keys());
3333
}
3434
navigate::Position::Internal(_) => {}
35-
navigate::Position::InternalKV(kv) => {
36-
let depth = self.height() - kv.into_node().height();
37-
let indent = " ".repeat(depth);
38-
result += &format!("\n{}{:?}", indent, kv.into_kv().0);
39-
}
35+
navigate::Position::InternalKV => {}
4036
});
4137
result
4238
}

library/std/src/sys/pal/unix/thread_local_dtor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
3434
#[cfg(not(sanitizer_cfi_normalize_integers))]
3535
#[cfi_encoding = "i"]
3636
#[repr(transparent)]
37-
pub struct c_int(pub libc::c_int);
37+
pub struct c_int(#[allow(dead_code)] pub libc::c_int);
3838

3939
extern "C" {
4040
#[linkage = "extern_weak"]

library/test/src/console.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ pub struct ConsoleTestDiscoveryState {
4646
pub tests: usize,
4747
pub benchmarks: usize,
4848
pub ignored: usize,
49-
pub options: Options,
5049
}
5150

5251
impl ConsoleTestDiscoveryState {
@@ -56,13 +55,7 @@ impl ConsoleTestDiscoveryState {
5655
None => None,
5756
};
5857

59-
Ok(ConsoleTestDiscoveryState {
60-
log_out,
61-
tests: 0,
62-
benchmarks: 0,
63-
ignored: 0,
64-
options: opts.options,
65-
})
58+
Ok(ConsoleTestDiscoveryState { log_out, tests: 0, benchmarks: 0, ignored: 0 })
6659
}
6760

6861
pub fn write_log<F, S>(&mut self, msg: F) -> io::Result<()>

src/tools/clippy/clippy_lints/src/raw_strings.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl EarlyLintPass for RawStrings {
108108
}
109109
}
110110

111-
let req = {
111+
let mut req = {
112112
let mut following_quote = false;
113113
let mut req = 0;
114114
// `once` so a raw string ending in hashes is still checked
@@ -136,7 +136,9 @@ impl EarlyLintPass for RawStrings {
136136
ControlFlow::Continue(num) | ControlFlow::Break(num) => num,
137137
}
138138
};
139-
139+
if self.allow_one_hash_in_raw_strings {
140+
req = req.max(1);
141+
}
140142
if req < max {
141143
span_lint_and_then(
142144
cx,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow-one-hash-in-raw-strings = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![allow(clippy::no_effect, unused)]
2+
#![warn(clippy::needless_raw_string_hashes)]
3+
4+
fn main() {
5+
r#"\aaa"#;
6+
r#"\aaa"#;
7+
r#"Hello "world"!"#;
8+
r####" "### "## "# "####;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![allow(clippy::no_effect, unused)]
2+
#![warn(clippy::needless_raw_string_hashes)]
3+
4+
fn main() {
5+
r#"\aaa"#;
6+
r##"\aaa"##;
7+
r##"Hello "world"!"##;
8+
r######" "### "## "# "######;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error: unnecessary hashes around raw string literal
2+
--> $DIR/needless_raw_string_hashes.rs:6:5
3+
|
4+
LL | r##"\aaa"##;
5+
| ^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::needless-raw-string-hashes` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::needless_raw_string_hashes)]`
9+
help: remove one hash from both sides of the string literal
10+
|
11+
LL - r##"\aaa"##;
12+
LL + r#"\aaa"#;
13+
|
14+
15+
error: unnecessary hashes around raw string literal
16+
--> $DIR/needless_raw_string_hashes.rs:7:5
17+
|
18+
LL | r##"Hello "world"!"##;
19+
| ^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
help: remove one hash from both sides of the string literal
22+
|
23+
LL - r##"Hello "world"!"##;
24+
LL + r#"Hello "world"!"#;
25+
|
26+
27+
error: unnecessary hashes around raw string literal
28+
--> $DIR/needless_raw_string_hashes.rs:8:5
29+
|
30+
LL | r######" "### "## "# "######;
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
|
33+
help: remove 2 hashes from both sides of the string literal
34+
|
35+
LL - r######" "### "## "# "######;
36+
LL + r####" "### "## "# "####;
37+
|
38+
39+
error: aborting due to 3 previous errors
40+

src/tools/miri/src/shims/unix/linux/fd/epoll.rs

+2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ pub struct Epoll {
2121
/// <https://man7.org/linux/man-pages/man2/epoll_ctl.2.html>
2222
#[derive(Clone, Debug)]
2323
pub struct EpollEvent {
24+
#[allow(dead_code)]
2425
pub events: u32,
2526
/// `Scalar<Provenance>` is used to represent the
2627
/// `epoll_data` type union.
28+
#[allow(dead_code)]
2729
pub data: Scalar<Provenance>,
2830
}
2931

tests/ui/impl-not-adjacent-to-type.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
mod foo {
44
pub struct Point {
55
pub x: i32,
6+
#[allow(dead_code)]
67
pub y: i32,
78
}
89
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![deny(dead_code)]
2+
3+
fn main() {
4+
let _ = foo::S{f: false};
5+
}
6+
7+
mod foo {
8+
pub struct S {
9+
pub f: bool, //~ ERROR field `f` is never read
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: field `f` is never read
2+
--> $DIR/pub-field-in-priv-mod.rs:9:13
3+
|
4+
LL | pub struct S {
5+
| - field in this struct
6+
LL | pub f: bool,
7+
| ^
8+
|
9+
note: the lint level is defined here
10+
--> $DIR/pub-field-in-priv-mod.rs:1:9
11+
|
12+
LL | #![deny(dead_code)]
13+
| ^^^^^^^^^
14+
15+
error: aborting due to 1 previous error
16+

tests/ui/privacy/suggest-making-field-public.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
mod a {
2+
pub mod a {
33
pub struct A(pub String);
44
}
55

tests/ui/privacy/suggest-making-field-public.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
mod a {
2+
pub mod a {
33
pub struct A(pub(self)String);
44
}
55

0 commit comments

Comments
 (0)