|
1 | 1 | //! Tests for `[lints]`
|
2 | 2 |
|
3 | 3 | use cargo_test_support::project;
|
| 4 | +use cargo_test_support::registry::Package; |
4 | 5 |
|
5 | 6 | #[cargo_test]
|
6 | 7 | fn package_requires_option() {
|
@@ -203,3 +204,297 @@ Caused by:
|
203 | 204 | )
|
204 | 205 | .run();
|
205 | 206 | }
|
| 207 | + |
| 208 | +#[cargo_test] |
| 209 | +fn package_lint_deny() { |
| 210 | + let foo = project() |
| 211 | + .file( |
| 212 | + "Cargo.toml", |
| 213 | + r#" |
| 214 | + cargo-features = ["lints"] |
| 215 | +
|
| 216 | + [package] |
| 217 | + name = "foo" |
| 218 | + version = "0.0.1" |
| 219 | + authors = [] |
| 220 | +
|
| 221 | + [lints.rust] |
| 222 | + "unsafe_code" = "deny" |
| 223 | + "#, |
| 224 | + ) |
| 225 | + .file( |
| 226 | + "src/lib.rs", |
| 227 | + " |
| 228 | +pub fn foo(num: i32) -> u32 { |
| 229 | + unsafe { std::mem::transmute(num) } |
| 230 | +} |
| 231 | +", |
| 232 | + ) |
| 233 | + .build(); |
| 234 | + |
| 235 | + foo.cargo("check") |
| 236 | + .masquerade_as_nightly_cargo(&["lints"]) |
| 237 | + .with_status(101) |
| 238 | + .with_stderr( |
| 239 | + "\ |
| 240 | +[..] |
| 241 | +error: usage of an `unsafe` block |
| 242 | +[..] |
| 243 | +[..] |
| 244 | +[..] |
| 245 | +[..] |
| 246 | +[..] |
| 247 | +[..] |
| 248 | +[..] |
| 249 | +error: could not compile `foo` (lib) due to previous error |
| 250 | +", |
| 251 | + ) |
| 252 | + .run(); |
| 253 | +} |
| 254 | + |
| 255 | +#[cargo_test] |
| 256 | +fn workspace_lint_deny() { |
| 257 | + let foo = project() |
| 258 | + .file( |
| 259 | + "Cargo.toml", |
| 260 | + r#" |
| 261 | + cargo-features = ["lints"] |
| 262 | +
|
| 263 | + [package] |
| 264 | + name = "foo" |
| 265 | + version = "0.0.1" |
| 266 | + authors = [] |
| 267 | +
|
| 268 | + [lints] |
| 269 | + workspace = true |
| 270 | +
|
| 271 | + [workspace.lints.rust] |
| 272 | + "unsafe_code" = "deny" |
| 273 | + "#, |
| 274 | + ) |
| 275 | + .file( |
| 276 | + "src/lib.rs", |
| 277 | + " |
| 278 | +pub fn foo(num: i32) -> u32 { |
| 279 | + unsafe { std::mem::transmute(num) } |
| 280 | +} |
| 281 | +", |
| 282 | + ) |
| 283 | + .build(); |
| 284 | + |
| 285 | + foo.cargo("check") |
| 286 | + .masquerade_as_nightly_cargo(&["lints"]) |
| 287 | + .with_status(101) |
| 288 | + .with_stderr( |
| 289 | + "\ |
| 290 | +[..] |
| 291 | +error: usage of an `unsafe` block |
| 292 | +[..] |
| 293 | +[..] |
| 294 | +[..] |
| 295 | +[..] |
| 296 | +[..] |
| 297 | +[..] |
| 298 | +[..] |
| 299 | +error: could not compile `foo` (lib) due to previous error |
| 300 | +", |
| 301 | + ) |
| 302 | + .run(); |
| 303 | +} |
| 304 | + |
| 305 | +#[cargo_test] |
| 306 | +fn attribute_has_precedence() { |
| 307 | + let foo = project() |
| 308 | + .file( |
| 309 | + "Cargo.toml", |
| 310 | + r#" |
| 311 | + cargo-features = ["lints"] |
| 312 | +
|
| 313 | + [package] |
| 314 | + name = "foo" |
| 315 | + version = "0.0.1" |
| 316 | + authors = [] |
| 317 | +
|
| 318 | + [lints.rust] |
| 319 | + "unsafe_code" = "deny" |
| 320 | + "#, |
| 321 | + ) |
| 322 | + .file( |
| 323 | + "src/lib.rs", |
| 324 | + " |
| 325 | +#![allow(unsafe_code)] |
| 326 | +
|
| 327 | +pub fn foo(num: i32) -> u32 { |
| 328 | + unsafe { std::mem::transmute(num) } |
| 329 | +} |
| 330 | +", |
| 331 | + ) |
| 332 | + .build(); |
| 333 | + |
| 334 | + foo.cargo("check") |
| 335 | + .masquerade_as_nightly_cargo(&["lints"]) |
| 336 | + .with_status(0) |
| 337 | + .run(); |
| 338 | +} |
| 339 | + |
| 340 | +#[cargo_test] |
| 341 | +fn rustflags_has_precedence() { |
| 342 | + let foo = project() |
| 343 | + .file( |
| 344 | + "Cargo.toml", |
| 345 | + r#" |
| 346 | + cargo-features = ["lints"] |
| 347 | +
|
| 348 | + [package] |
| 349 | + name = "foo" |
| 350 | + version = "0.0.1" |
| 351 | + authors = [] |
| 352 | +
|
| 353 | + [lints.rust] |
| 354 | + "unsafe_code" = "deny" |
| 355 | + "#, |
| 356 | + ) |
| 357 | + .file( |
| 358 | + "src/lib.rs", |
| 359 | + " |
| 360 | +pub fn foo(num: i32) -> u32 { |
| 361 | + unsafe { std::mem::transmute(num) } |
| 362 | +} |
| 363 | +", |
| 364 | + ) |
| 365 | + .build(); |
| 366 | + |
| 367 | + foo.cargo("check") |
| 368 | + .arg("-v") |
| 369 | + .env("RUSTFLAGS", "-Aunsafe_code") |
| 370 | + .masquerade_as_nightly_cargo(&["lints"]) |
| 371 | + .with_status(0) |
| 372 | + .run(); |
| 373 | +} |
| 374 | + |
| 375 | +#[cargo_test] |
| 376 | +fn without_priority() { |
| 377 | + Package::new("reg-dep", "1.0.0").publish(); |
| 378 | + |
| 379 | + let foo = project() |
| 380 | + .file( |
| 381 | + "Cargo.toml", |
| 382 | + r#" |
| 383 | + cargo-features = ["lints"] |
| 384 | +
|
| 385 | + [package] |
| 386 | + name = "foo" |
| 387 | + version = "0.0.1" |
| 388 | + edition = "2018" |
| 389 | + authors = [] |
| 390 | +
|
| 391 | + [dependencies] |
| 392 | + reg-dep = "1.0.0" |
| 393 | +
|
| 394 | + [lints.rust] |
| 395 | + "rust-2018-idioms" = "deny" |
| 396 | + "unused-extern-crates" = "allow" |
| 397 | + "#, |
| 398 | + ) |
| 399 | + .file( |
| 400 | + "src/lib.rs", |
| 401 | + " |
| 402 | +extern crate reg_dep; |
| 403 | +
|
| 404 | +pub fn foo() -> u32 { |
| 405 | + 2 |
| 406 | +} |
| 407 | +", |
| 408 | + ) |
| 409 | + .build(); |
| 410 | + |
| 411 | + foo.cargo("check") |
| 412 | + .masquerade_as_nightly_cargo(&["lints"]) |
| 413 | + .with_status(101) |
| 414 | + .with_stderr_contains( |
| 415 | + "\ |
| 416 | +error: unused extern crate |
| 417 | +", |
| 418 | + ) |
| 419 | + .run(); |
| 420 | +} |
| 421 | + |
| 422 | +#[cargo_test] |
| 423 | +fn with_priority() { |
| 424 | + Package::new("reg-dep", "1.0.0").publish(); |
| 425 | + |
| 426 | + let foo = project() |
| 427 | + .file( |
| 428 | + "Cargo.toml", |
| 429 | + r#" |
| 430 | + cargo-features = ["lints"] |
| 431 | +
|
| 432 | + [package] |
| 433 | + name = "foo" |
| 434 | + version = "0.0.1" |
| 435 | + edition = "2018" |
| 436 | + authors = [] |
| 437 | +
|
| 438 | + [dependencies] |
| 439 | + reg-dep = "1.0.0" |
| 440 | +
|
| 441 | + [lints.rust] |
| 442 | + "rust-2018-idioms" = { level = "deny", priority = -1 } |
| 443 | + "unused-extern-crates" = "allow" |
| 444 | + "#, |
| 445 | + ) |
| 446 | + .file( |
| 447 | + "src/lib.rs", |
| 448 | + " |
| 449 | +extern crate reg_dep; |
| 450 | +
|
| 451 | +pub fn foo() -> u32 { |
| 452 | + 2 |
| 453 | +} |
| 454 | +", |
| 455 | + ) |
| 456 | + .build(); |
| 457 | + |
| 458 | + foo.cargo("check") |
| 459 | + .masquerade_as_nightly_cargo(&["lints"]) |
| 460 | + .with_status(0) |
| 461 | + .run(); |
| 462 | +} |
| 463 | + |
| 464 | +#[cargo_test] |
| 465 | +fn rustdoc_lint() { |
| 466 | + let foo = project() |
| 467 | + .file( |
| 468 | + "Cargo.toml", |
| 469 | + r#" |
| 470 | + cargo-features = ["lints"] |
| 471 | +
|
| 472 | + [package] |
| 473 | + name = "foo" |
| 474 | + version = "0.0.1" |
| 475 | + authors = [] |
| 476 | +
|
| 477 | + [lints.rustdoc] |
| 478 | + broken_intra_doc_links = "deny" |
| 479 | + "#, |
| 480 | + ) |
| 481 | + .file( |
| 482 | + "src/lib.rs", |
| 483 | + " |
| 484 | +/// [`bar`] doesn't exist |
| 485 | +pub fn foo() -> u32 { |
| 486 | +} |
| 487 | +", |
| 488 | + ) |
| 489 | + .build(); |
| 490 | + |
| 491 | + foo.cargo("doc") |
| 492 | + .masquerade_as_nightly_cargo(&["lints"]) |
| 493 | + .with_status(101) |
| 494 | + .with_stderr_contains( |
| 495 | + "\ |
| 496 | +error: unresolved link to `bar` |
| 497 | +", |
| 498 | + ) |
| 499 | + .run(); |
| 500 | +} |
0 commit comments