-
Notifications
You must be signed in to change notification settings - Fork 73
feat(linter): Rule which warns when you could've used NoGcScope
#913
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
Open
eliassjogreen
wants to merge
6
commits into
trynova:main
Choose a base branch
from
eliassjogreen:feat/lint-can-use-no-gc-scope
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d3de701
feat(linter): Warn when you could've used NoGcScope
eliassjogreen e711f0e
feat(linter): Finish up lint logic
eliassjogreen 0898469
fix: `nova_vm` unnecessary uses of `GcScope`
eliassjogreen ad8120f
fix: Re-enable the rest of the lints
eliassjogreen 30ac4b3
fix: Opt-out of the lint in nova_cli
eliassjogreen 5cbb99a
fix: Aapos suggestions
eliassjogreen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| use std::ops::ControlFlow; | ||
|
|
||
| use clippy_utils::{ | ||
| diagnostics::span_lint_and_sugg, fn_def_id, is_trait_impl_item, source::HasSession, | ||
| visitors::for_each_expr, | ||
| }; | ||
| use rustc_errors::Applicability; | ||
| use rustc_hir::{Body, Expr, ExprKind, FnDecl, def::Res, def_id::LocalDefId, intravisit::FnKind}; | ||
| use rustc_hir_analysis::lower_ty; | ||
| use rustc_lint::{LateContext, LateLintPass}; | ||
| use rustc_span::{BytePos, Span}; | ||
|
|
||
| use crate::{could_be_builtin_method_def, is_gc_scope_ty, is_no_gc_method, is_trait_item}; | ||
|
|
||
| dylint_linting::declare_late_lint! { | ||
| /// ### What it does | ||
| /// | ||
| /// Checks that a function which only needs `NoGcScope` uses it instead of | ||
| /// `GcScope`. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// | ||
| /// You usually should use `NoGcScope` instead of `GcScope` if you don't | ||
| /// need the latter. The reason this is bad is that it forces the caller | ||
| /// to scope any heap references held past the call site unnecessarily. | ||
| /// | ||
| /// ### Example | ||
| /// | ||
| /// ```rust | ||
| /// fn foo(gc: GcScope<'_, '_>) {} | ||
| /// ``` | ||
| /// | ||
| /// Use instead: | ||
| /// | ||
| /// ```rust | ||
| /// fn foo(gc: NoGcScope<'_, '_>) {} | ||
| /// ``` | ||
| pub CAN_USE_NO_GC_SCOPE, | ||
| Warn, | ||
| "you should use `NoGcScope` instead of `GcScope` if you don't need the latter" | ||
| } | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for CanUseNoGcScope { | ||
| fn check_fn( | ||
| &mut self, | ||
| cx: &LateContext<'tcx>, | ||
| kind: FnKind<'tcx>, | ||
| _: &'tcx FnDecl<'tcx>, | ||
| body: &'tcx Body<'tcx>, | ||
| span: Span, | ||
| _: LocalDefId, | ||
| ) { | ||
| if span.from_expansion() { | ||
| return; | ||
| } | ||
|
|
||
| // Skip closures | ||
| if matches!(kind, FnKind::Closure) { | ||
| return; | ||
| } | ||
|
|
||
| // Skip trait definitions and methods | ||
| let res = { | ||
| let body_id = cx.tcx.hir_body_owner(body.id()); | ||
| is_trait_impl_item(cx, body_id) || is_trait_item(cx, body_id) | ||
| }; | ||
| if res { | ||
| return; | ||
| } | ||
|
|
||
| // Skip builtin methods | ||
| if could_be_builtin_method_def(cx, kind) { | ||
| return; | ||
| } | ||
|
|
||
| // Skip `GcScope` methods | ||
| if let FnKind::Method(_, sig) = kind | ||
| && let Some(maybe_self) = sig.decl.inputs.first() | ||
| && is_gc_scope_ty(cx, &lower_ty(cx.tcx, maybe_self)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| let typeck = cx.typeck_results(); | ||
|
|
||
| let Some(gc_scope) = body.params.iter().find(|param| { | ||
| let ty = typeck.pat_ty(param.pat); | ||
| is_gc_scope_ty(cx, &ty) | ||
| }) else { | ||
| // Either the function already takes `NoGcScope` or it doesn't take any | ||
| // at all in which case we don't need to lint. | ||
| return; | ||
| }; | ||
|
|
||
| if for_each_expr(cx, body.value, |expr| { | ||
| // Checks if the expression is function or method call | ||
| if let Some(did) = fn_def_id(cx, expr) | ||
| // If we encountered either a `nogc` och `into_nogc` method call | ||
| // we skip them because they don't count as needing a `GcScope`. | ||
| && !is_no_gc_method(cx, did) | ||
| { | ||
| // Check if the function actually uses `GcScope` in its signature | ||
| let sig = cx.tcx.fn_sig(did).instantiate_identity().skip_binder(); | ||
| if sig.inputs().iter().any(|input| is_gc_scope_ty(cx, input)) { | ||
| return ControlFlow::Break(()); | ||
| } | ||
| } | ||
|
|
||
| // Calls to closures and other functions may also use `GcScope`, | ||
| // we need to check those as well. | ||
| if let ExprKind::Call( | ||
| Expr { | ||
| kind: ExprKind::Path(qpath), | ||
| hir_id: path_hir_id, | ||
| .. | ||
| }, | ||
| args, | ||
| ) = expr.kind | ||
| && let Res::Local(_) = typeck.qpath_res(qpath, *path_hir_id) | ||
| && args.iter().any(|arg| { | ||
| let ty = typeck.expr_ty(arg); | ||
| is_gc_scope_ty(cx, &ty) | ||
| }) | ||
| { | ||
| return ControlFlow::Break(()); | ||
| } | ||
|
|
||
| ControlFlow::Continue(()) | ||
| }) | ||
| .is_none() | ||
| { | ||
| // We didn't find any calls in the body that would require a `GcScope` | ||
| // so we can suggest using `NoGcScope` instead. | ||
| let ty_span = cx | ||
| .sess() | ||
| .source_map() | ||
| .span_until_char(gc_scope.ty_span, '<'); | ||
| // Trim the start of the span to just before the `GcScope` type name | ||
| let ty_span = ty_span.with_lo(ty_span.hi() - BytePos(7)); | ||
| span_lint_and_sugg( | ||
| cx, | ||
| CAN_USE_NO_GC_SCOPE, | ||
| ty_span, | ||
| "you can use `NoGcScope` instead of `GcScope` here", | ||
| "replace with", | ||
| "NoGcScope".to_owned(), | ||
| Applicability::MaybeIncorrect, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.