rust-lang / rust-lang/rust-clippy

FN implicit if_same_then_else

Open
#3,030 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

fn main() {
	println!("{:?}", func(4));
}

fn func(n: i32) -> bool {
	if n < 2 {
		return true;
	} else if  n > 2 {
		return false;
	}
	return false; // return outside of conditions
}

this code is basically identical to

fn main() {
	println!("{:?}", func(4));
}

fn func(n: i32) -> bool {
	if n < 2 {
		return true;
	} else if  n > 2 {
		return false;
	} else { // return moved into else {}
		return false;
	}
}

but only the latter triggers the if_same_then_else warning.
Should the first example trigger a warning as well?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the if_same_then_else lint and its existing tests, then compare how the two Rust examples are analyzed. Add a regression case for the first example if it should produce the same warning, and run the lint's tests to verify the result.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.