-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
[Experimental] <T as Into<T>>::into
lint
#129249
Open
estebank
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
estebank:useless-into
base: master
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
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 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 |
---|---|---|
|
@@ -66,7 +66,7 @@ use crate::lints::{ | |
BuiltinTrivialBounds, BuiltinTypeAliasBounds, BuiltinUngatedAsyncFnTrackCaller, | ||
BuiltinUnpermittedTypeInit, BuiltinUnpermittedTypeInitSub, BuiltinUnreachablePub, | ||
BuiltinUnsafe, BuiltinUnstableFeatures, BuiltinUnusedDocComment, BuiltinUnusedDocCommentSub, | ||
BuiltinWhileTrue, InvalidAsmLabel, | ||
BuiltinWhileTrue, InvalidAsmLabel, SelfTypeConversionDiag, | ||
}; | ||
use crate::nonstandard_style::{method_context, MethodLateContext}; | ||
use crate::{ | ||
|
@@ -1604,6 +1604,7 @@ declare_lint_pass!( | |
SoftLints => [ | ||
WHILE_TRUE, | ||
NON_SHORTHAND_FIELD_PATTERNS, | ||
SELF_TYPE_CONVERSION, | ||
UNSAFE_CODE, | ||
MISSING_DOCS, | ||
MISSING_COPY_IMPLEMENTATIONS, | ||
|
@@ -3064,3 +3065,100 @@ impl EarlyLintPass for SpecialModuleName { | |
} | ||
} | ||
} | ||
|
||
declare_lint! { | ||
/// The `self_type_conversion` lint detects when a call to `.into()` does not have any effect. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust,compile_fail | ||
/// fn main() { | ||
/// let () = ().into(); | ||
/// } | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
pub SELF_TYPE_CONVERSION, | ||
Deny, | ||
"", | ||
} | ||
|
||
pub struct SelfTypeConversion<'tcx> { | ||
ignored_types: Vec<Ty<'tcx>>, | ||
} | ||
|
||
impl_lint_pass!(SelfTypeConversion<'_> => [SELF_TYPE_CONVERSION]); | ||
|
||
impl SelfTypeConversion<'_> { | ||
pub fn new() -> Self { | ||
Self { ignored_types: vec![] } | ||
} | ||
} | ||
|
||
impl<'tcx> LateLintPass<'tcx> for SelfTypeConversion<'tcx> { | ||
fn check_item_post(&mut self, cx: &LateContext<'tcx>, item: &hir::Item<'_>) { | ||
let hir::ItemKind::Use(path, kind) = item.kind else { return }; | ||
tracing::info!("{:#?}", item); | ||
tracing::info!(?path, ?kind); | ||
for res in &path.res { | ||
let Res::Def(DefKind::TyAlias, def_id) = res else { continue }; | ||
let ty = cx.tcx.type_of(def_id).instantiate_identity(); | ||
let name = cx.tcx.item_name(*def_id); | ||
// println!("{ty:?} {name:?}"); | ||
self.ignored_types.push(ty); | ||
for stripped in cx.tcx.stripped_cfg_items(def_id.krate) { | ||
if stripped.name.name == name { | ||
tracing::info!("{name:#?}"); | ||
} | ||
} | ||
} | ||
} | ||
Comment on lines
+3102
to
+3118
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. This is not the right way to do this check, but it was the fastest way of getting a semi-accurate discard of types that have |
||
|
||
fn check_expr_post(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) { | ||
let hir::ExprKind::MethodCall(_segment, rcvr, args, _) = expr.kind else { return }; | ||
if !args.is_empty() { | ||
tracing::info!("non-empty args"); | ||
return; | ||
} | ||
let ty = cx.typeck_results().expr_ty(expr); | ||
let rcvr_ty = cx.typeck_results().expr_ty(rcvr); | ||
tracing::info!(?ty, ?rcvr_ty); | ||
|
||
if ty != rcvr_ty { | ||
tracing::info!("different types"); | ||
return; | ||
} | ||
if self.ignored_types.contains(&rcvr_ty) { | ||
return; | ||
} | ||
let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) else { | ||
tracing::info!("no type dependent def id"); | ||
return; | ||
}; | ||
tracing::info!(?def_id); | ||
if Some(def_id) != cx.tcx.get_diagnostic_item(sym::into_fn) { | ||
tracing::info!("not into_fn {:?}", cx.tcx.get_diagnostic_item(sym::into_fn)); | ||
return; | ||
} | ||
tracing::info!(?def_id); | ||
tracing::info!(?expr); | ||
if expr.span.macro_backtrace().next().is_some() { | ||
return; | ||
} | ||
if cx.tcx.sess.source_map().span_to_embeddable_string(expr.span).contains("symbolize/gimli") | ||
{ | ||
// HACK | ||
return; | ||
} | ||
// println!("{:#?}", self.ignored_types); | ||
cx.emit_span_lint( | ||
SELF_TYPE_CONVERSION, | ||
expr.span, | ||
SelfTypeConversionDiag { source: rcvr_ty, target: ty }, | ||
); | ||
// bug!("asdf"); | ||
} | ||
} |
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect sentence structure...
(taken from https://github.com/rust-lang/rust-clippy/blob/e5a1ef0795a119a68cbd4cccb41b569206d74d78/clippy_lints/src/useless_conversion.rs#L187)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lint itself will need to be iterated on. This is all a placeholder so that I can run crater :)