Skip to content

Flag for enforcing explicit 'return' keyword #4359

Closed
@UebelAndre

Description

@UebelAndre

Is there a flag that would cause rustfmt to appropriately add an explicit return keyword at the end of functions?

Before:

fn is_divisible_by(lhs: u32, rhs: u32) -> bool {
    if rhs == 0 {
        return false;
    }

    lhs % rhs == 0
}

After:

fn is_divisible_by(lhs: u32, rhs: u32) -> bool {
    if rhs == 0 {
        return false;
    }

    return lhs % rhs == 0;
}

Diff:

diff --git a/example.rs b/example.rs
index 96b10a6..5af00c8 100644
--- a/example.rs
+++ b/example.rs
@@ -3,5 +3,5 @@ fn is_divisible_by(lhs: u32, rhs: u32) -> bool {
         return false;
     }

-    lhs % rhs == 0
+    return lhs % rhs == 0;
 }
\ No newline at end of file

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions