rust-lang / rust-lang/rust-clippy

Remove warning #comparison_chain

Open
#10,745 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

Tested using the following code on 1.71 nightly, performance is exactly the same now.

running 2 tests
test bench_f1 ... bench:         684 ns/iter (+/- 30)
test bench_f2 ... bench:         680 ns/iter (+/- 12)
Reproducer

I tried this code:

#![feature(test)]
extern crate test;

use std::cmp::Ordering;
use std::hint::black_box;
use std::fs::File;
use std::io::{Read, Write, Result};
use std::path::Path;

use test::Bencher;

fn f1(x: u8, y: u8) -> u8 {
    if x > y {
        a()
    } else if x < y {
        b()
    } else {
        c()
    }
}

fn f2(x: u8, y: u8) -> u8 {
    match x.cmp(&y) {
        Ordering::Greater => a(),
        Ordering::Less => b(),
        Ordering::Equal => c(),
    }
}

fn a() -> u8 {
    let mut file = File::create("/dev/null").unwrap();
    let output = black_box(1);
    file.write_all(&[output]).unwrap();
    output
}

fn b() -> u8 {
    let mut file = File::create("/dev/null").unwrap();
    let output = black_box(2);
    file.write_all(&[output]).unwrap();
    output
}

fn c() -> u8 {
    let mut file = File::create("/dev/null").unwrap();
    let output = black_box(3);
    file.write_all(&[output]).unwrap();
    output
}

#[bench]
fn bench_f1(b: &mut Bencher) {
    let mut input = [0u8; 2];
    File::open("/dev/urandom")
        .unwrap()
        .read_exact(&mut input)
        .unwrap();
    let x = black_box(input[0]);
    let y = black_box(input[1]);
    b.iter(|| f1(x, y));
}

#[bench]
fn bench_f2(b: &mut Bencher) {
    let mut input = [0u8; 2];
    File::open("/dev/urandom")
        .unwrap()
        .read_exact(&mut input)
        .unwrap();
    let x = black_box(input[0]);
    let y = black_box(input[1]);
    b.iter(|| f2(x, y));
}
Version
1.71 nightly on x86_64 WSL2
Additional Labels

No response

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 comparison_chain warning implementation and reproduce the issue with the supplied f1 and f2 benchmark code on Rust 1.71 nightly. Compare the lint's behavior with the benchmark results; done when the warning is removed or its outdated performance guidance is no longer emitted, with the relevant tests updated.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.