@@ -182,17 +182,17 @@ impl LateLintPass<'_> for NonCanonicalImpls {
182
182
183
183
if block. stmts . is_empty ( )
184
184
&& let Some ( expr) = block. expr
185
- && let ExprKind :: Call (
186
- Expr {
187
- kind : ExprKind :: Path ( some_path ) ,
188
- hir_id : some_hir_id ,
189
- ..
190
- } ,
191
- [ cmp_expr ] ,
192
- ) = expr . kind
193
- && is_res_lang_ctor ( cx , cx . qpath_res ( some_path , * some_hir_id ) , LangItem :: OptionSome )
194
- // Fix #11178, allow `Self::cmp(self, ..)` too
195
- && self_cmp_call ( cx, cmp_expr , impl_item. owner_id . def_id , & mut needs_fully_qualified)
185
+ && expr_is_cmp ( cx , & expr . kind , impl_item , & mut needs_fully_qualified )
186
+ {
187
+ }
188
+ // Fix #12683, allow [`needless_return`] here
189
+ else if block . expr . is_none ( )
190
+ && let Some ( stmt ) = block . stmts . first ( )
191
+ && let rustc_hir :: StmtKind :: Semi ( Expr {
192
+ kind : ExprKind :: Ret ( Some ( Expr { kind : ret_kind , .. } ) ) ,
193
+ ..
194
+ } ) = stmt . kind
195
+ && expr_is_cmp ( cx, ret_kind , impl_item, & mut needs_fully_qualified)
196
196
{
197
197
} else {
198
198
// If `Self` and `Rhs` are not the same type, bail. This makes creating a valid
@@ -245,6 +245,30 @@ impl LateLintPass<'_> for NonCanonicalImpls {
245
245
}
246
246
}
247
247
248
+ /// Return true if `expr_kind` is a `cmp` call.
249
+ fn expr_is_cmp < ' tcx > (
250
+ cx : & LateContext < ' tcx > ,
251
+ expr_kind : & ' tcx ExprKind < ' tcx > ,
252
+ impl_item : & ImplItem < ' _ > ,
253
+ needs_fully_qualified : & mut bool ,
254
+ ) -> bool {
255
+ if let ExprKind :: Call (
256
+ Expr {
257
+ kind : ExprKind :: Path ( some_path) ,
258
+ hir_id : some_hir_id,
259
+ ..
260
+ } ,
261
+ [ cmp_expr] ,
262
+ ) = expr_kind
263
+ {
264
+ is_res_lang_ctor ( cx, cx. qpath_res ( some_path, * some_hir_id) , LangItem :: OptionSome )
265
+ // Fix #11178, allow `Self::cmp(self, ..)` too
266
+ && self_cmp_call ( cx, cmp_expr, impl_item. owner_id . def_id , needs_fully_qualified)
267
+ } else {
268
+ false
269
+ }
270
+ }
271
+
248
272
/// Returns whether this is any of `self.cmp(..)`, `Self::cmp(self, ..)` or `Ord::cmp(self, ..)`.
249
273
fn self_cmp_call < ' tcx > (
250
274
cx : & LateContext < ' tcx > ,
0 commit comments