Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unsafe lint will also check for other unsafe code
Checks include declaration/implementation of unsafe functions, traits, and methods. This allows warning or forbidding all uses of unsafe code, whereas previously only unsafe blocks were caught by the lint. The lint has been renamed from `unsafe-blocks` to `unsafe-code` to reflect its new purpose. This is a minor [breaking-change] Closes #22430
- Loading branch information
Showing
4 changed files
with
103 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![allow(unused_unsafe)] | ||
#![allow(dead_code)] | ||
#![deny(unsafe_code)] | ||
|
||
struct Bar; | ||
|
||
#[allow(unsafe_code)] | ||
mod allowed_unsafe { | ||
fn allowed() { unsafe {} } | ||
unsafe fn also_allowed() {} | ||
unsafe trait AllowedUnsafe {} | ||
unsafe impl AllowedUnsafe for super::Bar {} | ||
} | ||
|
||
macro_rules! unsafe_in_macro { | ||
() => { | ||
unsafe {} //~ ERROR: usage of an `unsafe` block | ||
} | ||
} | ||
|
||
unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function | ||
unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait | ||
unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait | ||
|
||
trait Baz { | ||
unsafe fn baz(&self); //~ ERROR: declaration of an `unsafe` method | ||
unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method | ||
unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method | ||
} | ||
|
||
impl Baz for Bar { | ||
unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method | ||
unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method | ||
} | ||
|
||
fn main() { | ||
unsafe {} //~ ERROR: usage of an `unsafe` block | ||
|
||
unsafe_in_macro!() | ||
} |