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

Option to disable trivially_copy_pass_by_ref in public functions #4504

Closed
mati865 opened this issue Sep 5, 2019 · 13 comments · Fixed by #7187
Closed

Option to disable trivially_copy_pass_by_ref in public functions #4504

mati865 opened this issue Sep 5, 2019 · 13 comments · Fixed by #7187
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@mati865
Copy link
Contributor

mati865 commented Sep 5, 2019

Running Clippy over Rust repo yields about 100 unique warnings from trivially_copy_pass_by_ref, almost all of them were tripped by public functions which are not going to change.

Having option to disable it for public functions would help if this and probably other cases.

EDIT: ptr_arg have similar issue (but there are only 13 occurrences). Maybe something more generic should be developed?

@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Sep 5, 2019
@flip1995

This comment has been minimized.

@flip1995
Copy link
Member

flip1995 commented Sep 5, 2019

If this doesn't exist already, a function visibility(hir_id) would be useful in utils.

@flip1995
Copy link
Member

flip1995 commented Sep 5, 2019

Just realized, that there is a function visibility, which should be used here 😄

@palash25
Copy link

palash25 commented Oct 1, 2019

Hi @flip1995 new to Rust and I wanted to take a crack at this. I saw your comment explaining what needs to be done but its marked outdated anad collapsed, any reason for that? Is that approach not to be followed anymore?

@flip1995
Copy link
Member

flip1995 commented Oct 1, 2019

Hey @palash25 See the comment (#4504 (comment)) above yours. This isn't necessary, since the rust compiler already provides a function for that.

@palash25
Copy link

palash25 commented Oct 1, 2019

Oh my bad 😅 got confused.

Just to be sure I understand your comments correctly, the desired approach is to use the visibility method to figure out whether the method is public or not and if it is based on the flag disable or enable the lint, right?

@flip1995
Copy link
Member

flip1995 commented Oct 1, 2019

Yes correct. visibility takes a DefId. In the current implementation you have a HirId, which you can convert with opt_local_def_id.

based on the flag disable or enable the lint

Yes, I'd suggest to enable/disable the visibility check:

if allow_public_flag && is_public() {
    return;
}

@flip1995
Copy link
Member

flip1995 commented Oct 2, 2019

@palash25 I just realized, that this is even easier:

You can just use the function cx.access_levels.is_exported() for visibility.

@palash25
Copy link

palash25 commented Oct 2, 2019

thanks for your help @flip1995 will try to take a stab at this in a day or two

@palash25
Copy link

palash25 commented Oct 9, 2019

@flip1995 need a little help with this part allow_public_flag I saw clippy help and it mentions the following flags

    -W --warn OPT       Set lint warnings
    -A --allow OPT      Set lint allowed
    -D --deny OPT       Set lint denied
    -F --forbid OPT     Set lint forbidden

Do I have to somehow reuse allow flag (because it seems lint specific so doesn't make much sense to me) for public functions or implement a new flag for it?

@palash25
Copy link

palash25 commented Oct 9, 2019

Actually the clippy help text isn't very helpful I am not able to find out what could be the possible values for OPT I tried running something like cargo clippy --allow mut_from_ref but got an error. Is this documented in detail somewhere somewhere, any examples of flag usage?

@mati865
Copy link
Contributor Author

mati865 commented Oct 9, 2019

@palash25 it's explained in the readme: https://github.com/rust-lang/rust-clippy#allowingdenying-lints
TL;DR the syntax is as follows: cargo clippy -- -A clippy::lint_name.

@flip1995
Copy link
Member

Also this is not the place to look for this kind of configuration.

See for example this lint, for how to use a configuration:

pub struct Functions {
threshold: u64,
max_lines: u64,
}
impl Functions {
pub fn new(threshold: u64, max_lines: u64) -> Self {
Self { threshold, max_lines }
}
}

reg.register_late_lint_pass(box functions::Functions::new(conf.too_many_arguments_threshold, conf.too_many_lines_threshold));

And this for how to add a configuration option:

/// Lint: TOO_MANY_ARGUMENTS. The maximum number of argument a function or method can have
(too_many_arguments_threshold, "too_many_arguments_threshold", 7 => u64),

To test it, you have to create a ui-toml test, like this one:
https://github.com/rust-lang/rust-clippy/tree/master/tests/ui-toml/toml_trivially_copy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants