Unused import warning

Hi,

I am getting unused import error while running the tests. If I remove that import my crate fails on runnign the test
since some tests depend on such imports. I don't understand how to remove such warning.

use std::f32::consts::{FRAC_1_PI, PI};

pub struct CubicKernel {
    dim: usize,
    sigma: f32,
}

impl CubicKernel {
    pub fn new(dim: usize) -> Self {
        let sigma = if dim == 1 { 1. } else { 15. / 7. * FRAC_1_PI };

        CubicKernel { dim, sigma }
    }

    /// Given the ratio of rij and h return the value of the kernel value
    pub fn get_kernel_value(&self, rij: f32, h: f32) -> f32 {
        let h1 = 1. / h;
        let h_dim = h1.powi(self.dim as i32);
        let q = rij * h1;

        let fac = self.sigma * h_dim;

        let val = if q > 2. {
            0.
        } else if q > 1. {
            0.1666666666666 * (2. - q).powf(3.)
        } else {
            0.6666666666666 - q.powf(2.) + 0.5 * q.powf(3.)
        };

        fac * val
    }

    pub fn get_dwdq(&self, rij: f32, h:f32) -> f32{
        let h1 = 1. / h;
        let h_dim = 1. / h.powi(self.dim as i32);
        let q = rij * h1;

        let fac = self.sigma * h_dim;

        let val = if rij > 1e-12 {
            if q > 2. {
                0.
            } else if q > 1. {
                -0.5 * (2. - q).powf(2.)
            } else {
                (-2. * q + 1.5 * q.powi(2))
            }
        } else {
            0.
        };

        fac * val

    }

    /// Given the ratio of rij and h return the value of the kernel value
    pub fn get_gradient(&self, x_ij: &[f32], rij: f32, h: f32) -> Vec<f32> {
        let h1 = 1. / h;
        let dw_dq = self.get_dwdq(rij, h);
        let tmp = if rij > 1e-12{
            dw_dq * h1 / rij
        }
        else{
            0.
        };

        vec![tmp * x_ij[0], tmp * x_ij[1]]
    }
}


pub fn gradient_approximation(
    x: &[f32],
    func: &Fn(f32) -> f32,
    h: &[f32],
    krnl: &CubicKernel,
    dx: f32,
) -> Vec<f32> {
    let real: Vec<_> = x.iter().map(|i| func(*i)).collect();
    let mut approx = vec![0.; x.len()];

    for i in 0..x.len() {
        let x_i = x[i];
        for j in 0..x.len() {
            let x_j = x[j];
            let x_ij = vec![x_i - x_j, 0.];
            let rij = (x_i - x_j).abs();

            // update gradient
            let grad = krnl.get_gradient(&x_ij, rij, h[i]);
            // println!("{:?}", grad);

            // use gadient
            approx[i] += dx * real[j] * grad[0];
        }
    }
    approx
}

pub mod tests {
    use super::{*};

    #[test]
    fn test_derivative_approximation_with_different_functions() {
        let mut x: Vec<f32> = vec![];
        let x_left_limit = -PI;
        let x_right_limit = PI;
    }
}

Output is

|  Personal desktop => cargo  test
    Blocking waiting for file lock on build directory
   Compiling simulica v0.1.0 (file:///home/dinesh/multiphaserust/simulica)
warning: unused `#[macro_use]` import
 --> src/lib.rs:1:1
  |
1 | #[macro_use]
  | ^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

warning: unused import: `PI`
 --> src/kernel.rs:1:35
  |
1 | use std::f32::consts::{FRAC_1_PI, PI};
  |                                   ^^

warning: unused import: `*`
   --> src/kernel.rs:122:17
    |
122 |     use super::{*};
    |                 ^

1 Like

I didn't read your post in detail but the important thing is that your code gets compiled separately for every configuration, e.g. test and non-test. The check for unnecessary imports is done separately for both.

So, similarly to other code you should use

#[cfg(test)]
use ...

and

#[cfg(not (test))]
use ...

to mark imports as test-only or non-test only

8 Likes

That works, thank you.

1 Like