-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
98 additions
and
86 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 was deleted.
Oops, something went wrong.
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,39 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_help, is_lang_item_or_ctor, last_path_segment, match_def_path, paths}; | ||
use rustc_hir::{ | ||
def::{DefKind, Res}, | ||
GenericArg, LangItem, QPath, Ty, TyKind, | ||
}; | ||
use rustc_lint::LateContext; | ||
|
||
use super::NULL_POINTER_OPTIMIZATION; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, ty: &Ty<'_>, qpath: &QPath<'_>, res: Res) -> bool { | ||
if let Res::Def(DefKind::Struct, def_id) = res { | ||
if !(is_lang_item_or_ctor(cx, def_id, LangItem::OwnedBox) || match_def_path(cx, def_id, &paths::PTR_NON_NULL)) { | ||
return false; | ||
} | ||
|
||
if let Some(args) = last_path_segment(qpath).args | ||
&& let GenericArg::Type(option_ty) = args.args[0] | ||
&& let TyKind::Path(option_qpath) = option_ty.kind | ||
&& let res = cx.qpath_res(&option_qpath, option_ty.hir_id) | ||
&& let Res::Def(.., def_id) = res | ||
&& is_lang_item_or_ctor(cx, def_id, LangItem::Option) | ||
{ | ||
let outer_ty = last_path_segment(qpath).ident.name; | ||
span_lint_and_help( | ||
cx, | ||
NULL_POINTER_OPTIMIZATION, | ||
ty.span, | ||
&format!("usage of `{outer_ty}<Option<T>>`"), | ||
None, | ||
&format!("consider using `Option<{outer_ty}<T>>` instead, as it will grant better performance. For more information, see\n\ | ||
https://doc.rust-lang.org/core/option/#representation"), | ||
); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
false | ||
} |
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