Skip to content

Commit 702bb4b

Browse files
committed
lint: Do not emit unused warnings for public items
1 parent 595cbaf commit 702bb4b

File tree

7 files changed

+18
-13
lines changed

7 files changed

+18
-13
lines changed

gcc/rust/checks/lints/rust-lint-scan-deadcode.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ScanDeadcode : public MarkLiveBase
5353
void visit (HIR::Function &function) override
5454
{
5555
HirId hirId = function.get_mappings ().get_hirid ();
56-
if (should_warn (hirId))
56+
if (should_warn (hirId) && !function.get_visibility ().is_public ())
5757
{
5858
if (mappings->is_impl_item (hirId))
5959
{
@@ -78,7 +78,7 @@ class ScanDeadcode : public MarkLiveBase
7878
void visit (HIR::StructStruct &stct) override
7979
{
8080
HirId hirId = stct.get_mappings ().get_hirid ();
81-
if (should_warn (hirId))
81+
if (should_warn (hirId) && !stct.get_visibility ().is_public ())
8282
{
8383
bool name_starts_underscore = stct.get_identifier ().at (0) == '_';
8484
if (!name_starts_underscore)
@@ -92,7 +92,8 @@ class ScanDeadcode : public MarkLiveBase
9292
for (auto &field : stct.get_fields ())
9393
{
9494
HirId field_hir_id = field.get_mappings ().get_hirid ();
95-
if (should_warn (field_hir_id))
95+
if (should_warn (field_hir_id)
96+
&& !field.get_visibility ().is_public ())
9697
{
9798
rust_warning_at (field.get_locus (), 0,
9899
"field is never read: %<%s%>",
@@ -106,7 +107,7 @@ class ScanDeadcode : public MarkLiveBase
106107
{
107108
// only warn tuple struct unconstructed, and ignoring unused field
108109
HirId hirId = stct.get_mappings ().get_hirid ();
109-
if (should_warn (hirId))
110+
if (should_warn (hirId) && !stct.get_visibility ().is_public ())
110111
{
111112
rust_warning_at (stct.get_locus (), 0,
112113
"struct is never constructed: %<%s%>",

gcc/testsuite/rust/compile/issue-1031.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ extern "rust-intrinsic" {
66
#[lang = "const_ptr"]
77
impl<T> *const T {
88
pub const unsafe fn offset(self, count: isize) -> *const T {
9-
// { dg-warning "associated function is never used" "" { target *-*-* } .-1 }
109
unsafe { offset(self, count) }
1110
}
1211

1312
pub const unsafe fn add(self, count: usize) -> Self {
14-
// { dg-warning "associated function is never used" "" { target *-*-* } .-1 }
1513
unsafe { self.offset(count as isize) }
1614
}
1715
}

gcc/testsuite/rust/compile/issue-1289.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ impl<T> *mut T {
2323
#[lang = "const_ptr"]
2424
impl<T> *const T {
2525
pub const unsafe fn offset(self, count: isize) -> *mut T {
26-
// { dg-warning "associated function is never used" "" { target *-*-* } .-1 }
2726
unsafe { intrinsics::offset(self, count) as *mut T }
2827
}
2928

3029
pub const unsafe fn add(self, count: usize) -> Self {
31-
// { dg-warning "associated function is never used" "" { target *-*-* } .-1 }
3230
unsafe { self.offset(count as isize) }
3331
}
3432
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub struct Foo(i8);
2+
struct Bar(pub i8); // { dg-warning "struct is never constructed: .Bar." }
3+
pub struct Baz {
4+
a: i32, // { dg-warning "field is never read: .a." }
5+
pub b: i32,
6+
}
7+
8+
pub fn foo() {}
9+
fn bar() {} // { dg-warning "function is never used: .bar." }

gcc/testsuite/rust/compile/test_mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
//! foo bar baz cake pizza carbs
44
55
pub struct Test(pub i32);
6-
// { dg-warning "struct is never constructed" "" { target *-*-* } .-1 }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pub fn square(num: i32) -> i32 { /* { dg-warning "used" } */
1+
pub fn square(num: i32) -> i32 {
22
r#num * num
3-
}
3+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pub fn plus(r#break: i32, r#unsafe: i32) -> i32 { /* { dg-warning "used" } */
1+
pub fn plus(r#break: i32, r#unsafe: i32) -> i32 {
22
r#break + r#unsafe
3-
}
3+
}

0 commit comments

Comments
 (0)