Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code that is only used in test cases can be put behind #[cfg(test)]. #5

Open
squell opened this issue Jan 19, 2024 · 4 comments
Open
Labels
good first issue Good for newcomers

Comments

@squell
Copy link
Collaborator

squell commented Jan 19, 2024

What the title says. I thought this would show up but in a test I'm running it didn't.

@squell
Copy link
Collaborator Author

squell commented Jan 19, 2024

Sorry, this only happened in a binding I was looking at where pub was used.

Confirmed; I think code that is only used in test cases should at the very least be behind a #[cfg(test)] protection. We could add a flag that adds these to the code (removing test-cases is perhaps not the best idea...)

@squell squell closed this as completed Jan 19, 2024
@squell squell reopened this Jan 19, 2024
@squell squell changed the title Code that is only used in test cases does not get removed? Code that is only used in test cases can also get removed Jan 19, 2024
@squell squell changed the title Code that is only used in test cases can also get removed Code that is only used in test cases can be put behind #[cfg(test)]. Jan 19, 2024
@squell squell added the good first issue Good for newcomers label Jan 19, 2024
@squell
Copy link
Collaborator Author

squell commented Jan 19, 2024

Example:

const X: usize = 0;
const Y: usize = 0;

#[test]
fn test() {
    let _ = Y;
}
fn main() {
    println!("Hello, world!");
}

A cargo minify will only remove X, not Y; I would expect a diagnostic that this code can be transformed into:

#[cfg(test)]
const Y: usize = 0;

#[test]
fn test() {
    let _ = Y;
}
fn main() {
    println!("Hello, world!");
}

@squell
Copy link
Collaborator Author

squell commented Jul 29, 2024

Another use case:

fn main() {
    println!("Hello, world!");
}

fn bar() -> bool {
    true
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn foo() {
	assert!(bar());
    }
}

@van-sprundel
Copy link
Contributor

This could be split into two features. One would be to check if it's only used in a test environment, and the other if it's only used inside a specific module (since it doesn't have to specifically be a test module). Am I getting that right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants