rust/tests/ui/misnamed_getters.rs

144 lines
3.1 KiB
Rust
Raw Normal View History

2022-11-01 12:39:36 -05:00
#![allow(unused)]
2022-11-02 03:01:33 -05:00
#![warn(clippy::misnamed_getters)]
2022-11-01 12:39:36 -05:00
struct A {
a: u8,
b: u8,
2022-11-01 13:07:51 -05:00
c: u8,
2022-11-01 12:39:36 -05:00
}
impl A {
fn a(&self) -> &u8 {
//~^ ERROR: getter function appears to return the wrong field
//~| NOTE: `-D clippy::misnamed-getters` implied by `-D warnings`
2022-11-01 12:39:36 -05:00
&self.b
}
2022-11-01 13:07:51 -05:00
fn a_mut(&mut self) -> &mut u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-01 13:07:51 -05:00
&mut self.b
}
fn b(self) -> u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-01 13:07:51 -05:00
self.a
}
fn b_mut(&mut self) -> &mut u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-01 13:07:51 -05:00
&mut self.a
}
fn c(&self) -> &u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-01 13:07:51 -05:00
&self.b
}
fn c_mut(&mut self) -> &mut u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-01 13:07:51 -05:00
&mut self.a
}
2022-11-01 12:39:36 -05:00
}
union B {
a: u8,
b: u8,
}
impl B {
unsafe fn a(&self) -> &u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-01 12:39:36 -05:00
&self.b
}
2022-11-01 13:07:51 -05:00
unsafe fn a_mut(&mut self) -> &mut u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-01 13:07:51 -05:00
&mut self.b
}
unsafe fn b(self) -> u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-01 13:07:51 -05:00
self.a
}
unsafe fn b_mut(&mut self) -> &mut u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-01 13:07:51 -05:00
&mut self.a
}
unsafe fn c(&self) -> &u8 {
&self.b
}
unsafe fn c_mut(&mut self) -> &mut u8 {
&mut self.a
}
2022-11-02 13:02:46 -05:00
unsafe fn a_unchecked(&self) -> &u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-02 13:02:46 -05:00
&self.b
}
unsafe fn a_unchecked_mut(&mut self) -> &mut u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-02 13:02:46 -05:00
&mut self.b
}
unsafe fn b_unchecked(self) -> u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-02 13:02:46 -05:00
self.a
}
unsafe fn b_unchecked_mut(&mut self) -> &mut u8 {
//~^ ERROR: getter function appears to return the wrong field
2022-11-02 13:02:46 -05:00
&mut self.a
}
unsafe fn c_unchecked(&self) -> &u8 {
&self.b
}
unsafe fn c_unchecked_mut(&mut self) -> &mut u8 {
&mut self.a
}
2022-11-01 12:39:36 -05:00
}
struct D {
d: u8,
inner: A,
2022-11-12 15:33:46 -06:00
}
impl core::ops::Deref for D {
type Target = A;
fn deref(&self) -> &A {
&self.inner
2022-11-12 15:33:46 -06:00
}
}
impl core::ops::DerefMut for D {
fn deref_mut(&mut self) -> &mut A {
&mut self.inner
}
}
impl D {
fn a(&self) -> &u8 {
//~^ ERROR: getter function appears to return the wrong field
&self.b
}
fn a_mut(&mut self) -> &mut u8 {
//~^ ERROR: getter function appears to return the wrong field
&mut self.b
}
fn d(&self) -> &u8 {
//~^ ERROR: getter function appears to return the wrong field
&self.b
}
fn d_mut(&mut self) -> &mut u8 {
//~^ ERROR: getter function appears to return the wrong field
&mut self.b
2022-11-12 15:33:46 -06:00
}
}
2022-11-01 12:39:36 -05:00
fn main() {
// test code goes here
}