-
Notifications
You must be signed in to change notification settings - Fork 13.4k
feat: rustc_pass_by_value lint attribute #92646
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c3e330
feat: pass_by_value lint attribute
mdibaiee 91ed689
rustc_pass_by_value lint: add test on custom types
mdibaiee 71e3314
rustc_pass_by_value remove dependency on rustc_diagnostic_item
mdibaiee a6762e9
rustc_pass_by_value: allow types with no parameters on self
mdibaiee 959bf2b
rustc_pass_by_value: handle generic and const type parameters
mdibaiee 2728af7
rustc_pass_by_value: handle inferred generic types (with _)
mdibaiee 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,98 @@ | ||||||
use crate::{LateContext, LateLintPass, LintContext}; | ||||||
use rustc_errors::Applicability; | ||||||
use rustc_hir as hir; | ||||||
use rustc_hir::def::Res; | ||||||
use rustc_hir::{GenericArg, PathSegment, QPath, TyKind}; | ||||||
use rustc_middle::ty; | ||||||
use rustc_span::symbol::sym; | ||||||
|
||||||
declare_tool_lint! { | ||||||
/// The `rustc_pass_by_value` lint marks a type with `#[rustc_pass_by_value]` requiring it to always be passed by value. | ||||||
/// This is usually used for types that are thin wrappers around references, so there is no benefit to an extra | ||||||
/// layer of indirection. (Example: `Ty` which is a reference to a `TyS`) | ||||||
pub rustc::PASS_BY_VALUE, | ||||||
Warn, | ||||||
"pass by reference of a type flagged as `#[rustc_pass_by_value]`", | ||||||
report_in_external_macro: true | ||||||
} | ||||||
|
||||||
declare_lint_pass!(PassByValue => [PASS_BY_VALUE]); | ||||||
|
||||||
impl<'tcx> LateLintPass<'tcx> for PassByValue { | ||||||
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx>) { | ||||||
match &ty.kind { | ||||||
TyKind::Rptr(_, hir::MutTy { ty: inner_ty, mutbl: hir::Mutability::Not }) => { | ||||||
if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner.to_def_id()) { | ||||||
if cx.tcx.impl_trait_ref(impl_did).is_some() { | ||||||
return; | ||||||
} | ||||||
} | ||||||
if let Some(t) = path_for_pass_by_value(cx, &inner_ty) { | ||||||
cx.struct_span_lint(PASS_BY_VALUE, ty.span, |lint| { | ||||||
lint.build(&format!("passing `{}` by reference", t)) | ||||||
.span_suggestion( | ||||||
ty.span, | ||||||
"try passing by value", | ||||||
t, | ||||||
// Changing type of function argument | ||||||
Applicability::MaybeIncorrect, | ||||||
) | ||||||
.emit(); | ||||||
}) | ||||||
} | ||||||
} | ||||||
_ => {} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<String> { | ||||||
if let TyKind::Path(QPath::Resolved(_, path)) = &ty.kind { | ||||||
match path.res { | ||||||
Res::Def(_, def_id) if cx.tcx.has_attr(def_id, sym::rustc_pass_by_value) => { | ||||||
let name = cx.tcx.item_name(def_id).to_ident_string(); | ||||||
let path_segment = path.segments.last().unwrap(); | ||||||
return Some(format!("{}{}", name, gen_args(cx, path_segment))); | ||||||
} | ||||||
Res::SelfTy(None, Some((did, _))) => { | ||||||
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() { | ||||||
if cx.tcx.has_attr(adt.did, sym::rustc_pass_by_value) { | ||||||
return Some(cx.tcx.def_path_str_with_substs(adt.did, substs)); | ||||||
} | ||||||
} | ||||||
} | ||||||
_ => (), | ||||||
} | ||||||
} | ||||||
|
||||||
None | ||||||
} | ||||||
|
||||||
fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String { | ||||||
if let Some(args) = &segment.args { | ||||||
let params = args | ||||||
.args | ||||||
.iter() | ||||||
.filter_map(|arg| match arg { | ||||||
GenericArg::Lifetime(lt) => Some(lt.name.ident().to_string()), | ||||||
GenericArg::Type(ty) => { | ||||||
let snippet = | ||||||
cx.tcx.sess.source_map().span_to_snippet(ty.span).unwrap_or_default(); | ||||||
Some(snippet) | ||||||
} | ||||||
GenericArg::Const(c) => { | ||||||
let snippet = | ||||||
cx.tcx.sess.source_map().span_to_snippet(c.span).unwrap_or_default(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
🤔 don't want empty strings in case the span is broken. Don't know how to even get broken strings here, so i don't think this matters too much :p |
||||||
Some(snippet) | ||||||
} | ||||||
_ => None, | ||||||
}) | ||||||
.collect::<Vec<_>>(); | ||||||
|
||||||
if !params.is_empty() { | ||||||
return format!("<{}>", params.join(", ")); | ||||||
} | ||||||
} | ||||||
|
||||||
String::new() | ||||||
} |
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
20 changes: 0 additions & 20 deletions
20
src/test/ui-fulldeps/internal-lints/pass_ty_by_ref_self.stderr
This file was deleted.
Oops, something went wrong.
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.