Skip to content

Why not use this "Circle :: new ()" #14770

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
electricface opened this issue Jun 9, 2014 · 1 comment
Closed

Why not use this "Circle :: new ()" #14770

electricface opened this issue Jun 9, 2014 · 1 comment

Comments

@electricface
Copy link

use std::f64::consts::PI;
trait Shape { fn new(area: f64) -> Self; }
struct Circle { radius: f64 }
struct Square { length: f64 }

impl Shape for Circle {
    fn new(area: f64) -> Circle { Circle { radius: (area / PI).sqrt() } }
}
impl Shape for Square {
    fn new(area: f64) -> Square { Square { length: area.sqrt() } }
}

let area = 42.5;
let c: Circle = Shape::new(area);
let s: Square = Shape::new(area);

Why is this wrong

    let c = Circle::new( area );
    let s = Square::new( area );
traits-test.rs:126:10: 126:21 error: unresolved name
traits-test.rs:126  let c = Circle::new( area );
                            ^~~~~~~~~~~
traits-test.rs:126:10: 126:21 error: use of undeclared module `Circle`
traits-test.rs:126  let c = Circle::new( area );
                            ^~~~~~~~~~~
traits-test.rs:126:10: 126:21 error: unresolved name `Circle::new`.
traits-test.rs:126  let c = Circle::new( area );
                            ^~~~~~~~~~~
traits-test.rs:127:10: 127:21 error: unresolved name
traits-test.rs:127  let s = Square::new( area );
                            ^~~~~~~~~~~
traits-test.rs:127:10: 127:21 error: use of undeclared module `Square`
traits-test.rs:127  let s = Square::new( area );
                            ^~~~~~~~~~~
traits-test.rs:127:10: 127:21 error: unresolved name `Square::new`.
traits-test.rs:127  let s = Square::new( area );
                            ^~~~~~~~~~~
error: aborting due to 6 previous errors
@huonw
Copy link
Member

huonw commented Jun 9, 2014

Trait methods are always called via the trait; we just don't have support for anything else yet.

Dupe of #6894. (Thanks for filing!)

@huonw huonw closed this as completed Jun 9, 2014
flip1995 pushed a commit to flip1995/rust that referenced this issue May 15, 2025
…ust-lang#14770)

The problem is that `check_fn` is triggered by both function and
closure, and `visit_expr` can visit expressions in another closures
within a function or closure.

So just skip walking in a inner closure.

changelog: Fix [`unnecessary_unwrap`] emitted twice in closure which
inside in a function or another closure.

Fixes: rust-lang/rust-clippy#14763
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants