Skip to content

Commit

Permalink
Use break api config for unnecessary_wraps
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed May 26, 2021
1 parent 3d77a2b commit 1ce581d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Expand Up @@ -1976,7 +1976,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| box redundant_clone::RedundantClone);
store.register_late_pass(|| box slow_vector_initialization::SlowVectorInit);
store.register_late_pass(|| box unnecessary_sort_by::UnnecessarySortBy);
store.register_late_pass(|| box unnecessary_wraps::UnnecessaryWraps);
store.register_late_pass(move || box unnecessary_wraps::UnnecessaryWraps::new(avoid_breaking_exported_api));
store.register_late_pass(|| box assertions_on_constants::AssertionsOnConstants);
store.register_late_pass(|| box transmuting_null::TransmutingNull);
store.register_late_pass(|| box path_buf_push_overwrite::PathBufPushOverwrite);
Expand Down
21 changes: 16 additions & 5 deletions clippy_lints/src/unnecessary_wraps.rs
Expand Up @@ -8,7 +8,7 @@ use rustc_hir::LangItem::{OptionSome, ResultOk};
use rustc_hir::{Body, ExprKind, FnDecl, HirId, Impl, ItemKind, Node};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::symbol::sym;
use rustc_span::Span;

Expand Down Expand Up @@ -52,7 +52,19 @@ declare_clippy_lint! {
"functions that only return `Ok` or `Some`"
}

declare_lint_pass!(UnnecessaryWraps => [UNNECESSARY_WRAPS]);
pub struct UnnecessaryWraps {
avoid_breaking_exported_api: bool,
}

impl_lint_pass!(UnnecessaryWraps => [UNNECESSARY_WRAPS]);

impl UnnecessaryWraps {
pub fn new(avoid_breaking_exported_api: bool) -> Self {
Self {
avoid_breaking_exported_api,
}
}
}

impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
fn check_fn(
Expand All @@ -66,13 +78,12 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
) {
// Abort if public function/method or closure.
match fn_kind {
FnKind::ItemFn(.., visibility) | FnKind::Method(.., Some(visibility)) => {
if visibility.node.is_pub() {
FnKind::ItemFn(..) | FnKind::Method(..) => {
if self.avoid_breaking_exported_api && cx.access_levels.is_exported(hir_id) {
return;
}
},
FnKind::Closure => return,
FnKind::Method(..) => (),
}

// Abort if the method is implementing a trait or of it a trait method.
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unnecessary_wraps.rs
Expand Up @@ -65,7 +65,7 @@ fn func10() -> Option<()> {
unimplemented!()
}

struct A;
pub struct A;

impl A {
// should not be linted
Expand Down

0 comments on commit 1ce581d

Please sign in to comment.