Skip to content

Commit e0d261e

Browse files
committed
auto merge of #13579 : hirschenberger/rust/lint_unsigned_negate, r=alexcrichton
See #11273 and #13318
2 parents b5d6b07 + 6c26cbb commit e0d261e

File tree

14 files changed

+58
-3
lines changed

14 files changed

+58
-3
lines changed

src/libnative/io/timer_win32.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl rtio::RtioTimer for Timer {
137137

138138
// there are 10^6 nanoseconds in a millisecond, and the parameter is in
139139
// 100ns intervals, so we multiply by 10^4.
140-
let due = -(msecs * 10000) as libc::LARGE_INTEGER;
140+
let due = -(msecs as i64 * 10000) as libc::LARGE_INTEGER;
141141
assert_eq!(unsafe {
142142
imp::SetWaitableTimer(self.obj, &due, 0, ptr::null(),
143143
ptr::mut_null(), 0)
@@ -151,7 +151,7 @@ impl rtio::RtioTimer for Timer {
151151
let (tx, rx) = channel();
152152

153153
// see above for the calculation
154-
let due = -(msecs * 10000) as libc::LARGE_INTEGER;
154+
let due = -(msecs as i64 * 10000) as libc::LARGE_INTEGER;
155155
assert_eq!(unsafe {
156156
imp::SetWaitableTimer(self.obj, &due, 0, ptr::null(),
157157
ptr::mut_null(), 0)
@@ -167,7 +167,7 @@ impl rtio::RtioTimer for Timer {
167167
let (tx, rx) = channel();
168168

169169
// see above for the calculation
170-
let due = -(msecs * 10000) as libc::LARGE_INTEGER;
170+
let due = -(msecs as i64 * 10000) as libc::LARGE_INTEGER;
171171
assert_eq!(unsafe {
172172
imp::SetWaitableTimer(self.obj, &due, msecs as libc::LONG,
173173
ptr::null(), ptr::mut_null(), 0)

src/librustc/middle/const_eval.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
#![allow(non_camel_case_types)]
12+
#![allow(unsigned_negate)]
1213

1314
use metadata::csearch;
1415
use middle::astencode;

src/librustc/middle/lint.rs

+31
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub enum Lint {
9191
AttributeUsage,
9292
UnknownFeatures,
9393
UnknownCrateType,
94+
UnsignedNegate,
9495

9596
ManagedHeapMemory,
9697
OwnedHeapMemory,
@@ -390,6 +391,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
390391
default: deny,
391392
}),
392393

394+
("unsigned_negate",
395+
LintSpec {
396+
lint: UnsignedNegate,
397+
desc: "using an unary minus operator on unsigned type",
398+
default: warn
399+
}),
400+
393401
("unused_must_use",
394402
LintSpec {
395403
lint: UnusedMustUse,
@@ -704,6 +712,29 @@ fn check_unused_casts(cx: &Context, e: &ast::Expr) {
704712

705713
fn check_type_limits(cx: &Context, e: &ast::Expr) {
706714
return match e.node {
715+
ast::ExprUnary(ast::UnNeg, ex) => {
716+
match ex.node {
717+
ast::ExprLit(lit) => {
718+
match lit.node {
719+
ast::LitUint(..) => {
720+
cx.span_lint(UnsignedNegate, e.span,
721+
"negation of unsigned int literal may be unintentional");
722+
},
723+
_ => ()
724+
}
725+
},
726+
_ => {
727+
let t = ty::expr_ty(cx.tcx, ex);
728+
match ty::get(t).sty {
729+
ty::ty_uint(_) => {
730+
cx.span_lint(UnsignedNegate, e.span,
731+
"negation of unsigned int variable may be unintentional");
732+
},
733+
_ => ()
734+
}
735+
}
736+
}
737+
},
707738
ast::ExprBinary(binop, l, r) => {
708739
if is_comparison(binop) && !check_limits(cx.tcx, binop, l, r) {
709740
cx.span_lint(TypeLimits, e.span,

src/librustc/middle/trans/adt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
* taken to it, implementing them for Rust seems difficult.
4444
*/
4545

46+
#![allow(unsigned_negate)]
47+
4648
use std::container::Map;
4749
use libc::c_ulonglong;
4850
use std::num::{Bitwise};

src/libstd/fmt/num.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
1313
// FIXME: #6220 Implement floating point formatting
1414

15+
#![allow(unsigned_negate)]
16+
1517
use container::Container;
1618
use fmt;
1719
use iter::{Iterator, DoubleEndedIterator};

src/libstd/num/f32.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Operations and constants for 32-bits floats (`f32` type)
1212
1313
#![allow(missing_doc)]
14+
#![allow(unsigned_negate)]
1415

1516
use prelude::*;
1617

src/libstd/num/u16.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Operations and constants for unsigned 16-bits integers (`u16` type)
1212
1313
#![allow(non_uppercase_statics)]
14+
#![allow(unsigned_negate)]
1415

1516
use prelude::*;
1617

src/libstd/num/u32.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Operations and constants for unsigned 32-bits integers (`u32` type)
1212
1313
#![allow(non_uppercase_statics)]
14+
#![allow(unsigned_negate)]
1415

1516
use prelude::*;
1617

src/libstd/num/u64.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Operations and constants for unsigned 64-bits integer (`u64` type)
1212
1313
#![allow(non_uppercase_statics)]
14+
#![allow(unsigned_negate)]
1415

1516
use prelude::*;
1617

src/libstd/num/u8.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Operations and constants for unsigned 8-bits integers (`u8` type)
1212
1313
#![allow(non_uppercase_statics)]
14+
#![allow(unsigned_negate)]
1415

1516
use prelude::*;
1617

src/libstd/num/uint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Operations and constants for architecture-sized unsigned integers (`uint` type)
1212
1313
#![allow(non_uppercase_statics)]
14+
#![allow(unsigned_negate)]
1415

1516
use prelude::*;
1617

src/libstd/num/uint_macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#![macro_escape]
1212
#![doc(hidden)]
13+
#![allow(unsigned_negate)]
1314

1415
macro_rules! uint_module (($T:ty, $T_SIGNED:ty, $bits:expr) => (
1516

src/libstd/rt/thread.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! which are not used for scheduling in any way.
1616
1717
#![allow(non_camel_case_types)]
18+
#![allow(unsigned_negate)]
1819

1920
use cast;
2021
use kinds::Send;

src/test/compile-fail/lint-type-limits.rs

+11
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,14 @@ fn qux() {
3636
i += 1;
3737
}
3838
}
39+
40+
fn quy() {
41+
let i = -23u; //~ WARNING negation of unsigned int literal may be unintentional
42+
//~^ WARNING unused variable
43+
}
44+
45+
fn quz() {
46+
let i = 23u;
47+
let j = -i; //~ WARNING negation of unsigned int variable may be unintentional
48+
//~^ WARNING unused variable
49+
}

0 commit comments

Comments
 (0)