#![warn(clippy::manual_div_ceil)]

macro_rules! y {
    () => {
        let x = 33u32;
        let _ = x.div_ceil(8);
        //~^ manual_div_ceil
        let _ = x.div_ceil(8);
        //~^ manual_div_ceil
    };
}

macro_rules! eight {
    () => {
        8
    };
}

macro_rules! plus_one {
    ($val:expr) => {
        ($val + 1)
    };
}

fn main() {
    let x = 7_u32;
    let y = 4_u32;
    let z = 11_u32;

    // Lint
    let _ = x.div_ceil(y);
    //~^ manual_div_ceil
    let _ = x.div_ceil(y);
    //~^ manual_div_ceil
    let _ = x.div_ceil(y);
    //~^ manual_div_ceil

    let _ = 7_u32.div_ceil(4);
    //~^ manual_div_ceil
    let _ = (7_i32 as u32).div_ceil(4);
    //~^ manual_div_ceil

    // No lint
    let _ = (x + (y - 2)) / y;
    let _ = (x + (y + 1)) / y;

    let _ = (x + (y - 1)) / z;

    let x_i = 7_i32;
    let y_i = 4_i32;
    let z_i = 11_i32;

    // No lint because `int_roundings` feature is not enabled.
    let _ = (z as i32 + (y_i - 1)) / y_i;
    let _ = (7_u32 as i32 + (y_i - 1)) / y_i;
    let _ = (7_u32 as i32 + (4 - 1)) / 4;

    // Test lint with macro
    y!();

    // Also test if RHS should be result of macro expansion
    let _ = 33u32.div_ceil(eight!());
    //~^ manual_div_ceil

    let _ = plus_one!(x).div_ceil(y);
    //~^ manual_div_ceil
}

fn issue_13843() {
    let x = 3usize;
    let _ = 2048_usize.div_ceil(x);
    //~^ manual_div_ceil

    let x = 5usize;
    let _ = 2048usize.div_ceil(x);
    //~^ manual_div_ceil

    let x = 5usize;
    let _ = 2048_usize.div_ceil(x);
    //~^ manual_div_ceil

    let x = 2048usize;
    let _ = x.div_ceil(4);
    //~^ manual_div_ceil

    let _: u32 = 2048_u32.div_ceil(6);
    //~^ manual_div_ceil
    let _: usize = 2048_usize.div_ceil(6);
    //~^ manual_div_ceil
    let _: u32 = 0x2048_u32.div_ceil(0x6);
    //~^ manual_div_ceil

    let _ = 2048_u32.div_ceil(6u32);
    //~^ manual_div_ceil

    let _ = 1_000_000_u32.div_ceil(6u32);
    //~^ manual_div_ceil
}

fn issue_13950() {
    let x = 33u32;
    let _ = x.div_ceil(8);
    //~^ manual_div_ceil
    let _ = x.div_ceil(8);
    //~^ manual_div_ceil

    let y = -33i32;
    let _ = (y + -8) / -7;
    let _ = (-8 + y) / -7;
    let _ = (y - 8) / -7;
}

fn issue_15705(size: u64, c: &u64) {
    let _ = size.div_ceil(*c);
    //~^ manual_div_ceil
}

struct MyStruct(u32);
impl MyStruct {
    // Method matching name on different type should not trigger lint
    fn next_multiple_of(self, y: u32) -> u32 {
        self.0.next_multiple_of(y)
    }
}

fn issue_16219() {
    let x = 33u32;

    // Lint.
    let _ = x.div_ceil(8);
    //~^ manual_div_ceil
    let _ = x.div_ceil(8);
    //~^ manual_div_ceil

    let y = &x;
    let _ = y.div_ceil(8);
    //~^ manual_div_ceil

    // No lint.
    let _ = x.next_multiple_of(8) / 7;
    let _ = x.next_multiple_of(7) / 8;

    let z = MyStruct(x);
    let _ = z.next_multiple_of(8) / 8;
}
