forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#92970 - matthiaskrgr:rollup-tcx7cfb, r=matthi…
…askrgr Rollup of 10 pull requests Successful merges: - rust-lang#92487 (Fix unclosed boxes in pretty printing of TraitAlias) - rust-lang#92581 (ARMv6K Horizon - Enable default libraries) - rust-lang#92619 (Add diagnostic items for macros) - rust-lang#92635 (rustdoc: Yet more intra-doc links cleanup) - rust-lang#92646 (feat: rustc_pass_by_value lint attribute) - rust-lang#92706 (Clarify explicitly that BTree{Map,Set} are ordered.) - rust-lang#92710 (Include Projections when elaborating TypeOutlives) - rust-lang#92746 (Parse `Ty?` as `Option<Ty>` and provide structured suggestion) - rust-lang#92792 (rustdoc: fix intra-link for generic trait impls) - rust-lang#92814 (remove unused FIXME) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
- Loading branch information
Showing
51 changed files
with
906 additions
and
362 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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,94 @@ | ||
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() | ||
.map(|arg| match arg { | ||
GenericArg::Lifetime(lt) => lt.name.ident().to_string(), | ||
GenericArg::Type(ty) => { | ||
cx.tcx.sess.source_map().span_to_snippet(ty.span).unwrap_or_default() | ||
} | ||
GenericArg::Const(c) => { | ||
cx.tcx.sess.source_map().span_to_snippet(c.span).unwrap_or_default() | ||
} | ||
GenericArg::Infer(_) => String::from("_"), | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
if !params.is_empty() { | ||
return format!("<{}>", params.join(", ")); | ||
} | ||
} | ||
|
||
String::new() | ||
} |
This file contains 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 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 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 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.